Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby tips : Case statement
lang = 'en'
welcome_msg = case lang
when 'en' then 'welcome'
when 'de' then 'willkommen'
when 'fr' then 'bienvenue'
when 'de' then 'bienvenida'
else 'yo'
end
lang = 'en'
# multiline case statement
case lang
when 'en'
puts 'welcome'
# ... more statements
when 'de'
puts 'willkommen'
# ... more statements
when 'fr'
puts 'bienvenue'
# ... more statements
else
'yo'
# ... more statements
end
# one-line case statement. Don't forget the 'then' keyword
case lang
when 'en' then puts 'welcome'
when 'de' then puts 'willkommen'
when 'fr' then puts 'bienvenue'
when 'de' then puts 'bienvenida'
else 'yo'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment