Created
August 21, 2011 16:37
-
-
Save jbrechtel/1160828 to your computer and use it in GitHub Desktop.
Ruby case statements
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
#execute a different method based on a variable value | |
name = ask_for_name() | |
case name | |
when 'bill' | |
email_report() | |
when 'thomas' | |
print_report() | |
when 'ted' | |
fax_report() | |
when 'james' | |
rsync_report() | |
end | |
#return a value from a case statement | |
location = ask_for_location() | |
address = case location | |
when 'home' | |
'555 Main St. Gulfport, MS 39503' | |
when 'office' | |
'930 28th St. Long Beach, MS 39520' | |
when 'school' | |
'200 Highway 90 Long Beach, MS 39520' | |
end | |
#match value based on regex | |
#extract middle name, if one was given. | |
#if one wasn't given then assume it should be Peatrice | |
middle_name = case full_name | |
when /^.*\s(.*)\s.*$/ | |
$1 | |
else | |
"Peatrice" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment