Created
September 13, 2015 11:23
-
-
Save tsevdos/1406c59721ba5000400e to your computer and use it in GitHub Desktop.
Ruby tips : Case statement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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