Skip to content

Instantly share code, notes, and snippets.

@tsevdos
Created September 13, 2015 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsevdos/1406c59721ba5000400e to your computer and use it in GitHub Desktop.
Save tsevdos/1406c59721ba5000400e to your computer and use it in GitHub Desktop.
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