Created
December 8, 2010 18:13
-
-
Save jmccartie/733658 to your computer and use it in GitHub Desktop.
converts state short name to long name
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
# Converts two-letter state to full-state name | |
def state_name(state_abbr) | |
states = {:AK => "Alaska", :AL => "Alabama", :AS => "American Samoa", :AZ => "Arizona", :AR => "Arkansas", :CA => "California", :CO => "Colorado", :CT => "Connecticut", :DE => "Delaware", :DC => "District of Columbia", :FM => "Federated States of Micronesia", :FL => "Florida", :GA => "Georgia", :GU => "Guam", :HI => "Hawaii", :ID => "Idaho", :IL => "Illinois", :IN => "Indiana", :IA => "Iowa", :KS => "Kansas", :KY => "Kentucky", :LA => "Louisiana", :ME => "Maine", :MH => "Marshall Islands", :MD => "Maryland", :MA => "Massachusetts", :MI => "Michigan", :MN => "Minnesota", :MS => "Mississippi", :MO => "Missouri", :MT => "Montana", :NE => "Nebraska", :NV => "Nevada", :NH => "New Hampshire", :NJ => "New Jersey", :NM => "New Mexico", :NY => "New York", :NC => "North Carolina", :ND => "North Dakota", :MP => "Northern Mariana Islands", :OH => "Ohio", :OK => "Oklahoma", :OR => "Oregon", :PW => "Palau", :PA => "Pennsylvania", :PR => "Puerto Rico", :RI => "Rhode Island", :SC => "South Carolina", :SD => "South Dakota", :TN => "Tennessee", :TX => "Texas", :UT => "Utah", :VT => "Vermont", :VI => "Virgin Islands", :VA => "Virginia", :WA => "Washington", :WV => "West Virginia", :WI => "Wisconsin", :WY => "Wyoming", :AE => "Armed Forces Middle East", :AA => "Armed Forces Americas (except Canada)", :AP => "Armed Forces Pacific"} | |
states[state_abbr] || state_abbr.to_s | |
end |
If you want to make it a bit more idiomatic Ruby, you almost never need return
unless you want to bail from a method early.
states = {:AK => "Alaska", :AL => "Alabama", :AS => "American Samoa", :AZ => "Arizona", :AR => "Arkansas", :CA => "California", :CO => "Colorado", :CT => "Connecticut", :DE => "Delaware", :DC => "District of Columbia", :FM => "Federated States of Micronesia", :FL => "Florida", :GA => "Georgia", :GU => "Guam", :HI => "Hawaii", :ID => "Idaho", :IL => "Illinois", :IN => "Indiana", :IA => "Iowa", :KS => "Kansas", :KY => "Kentucky", :LA => "Louisiana", :ME => "Maine", :MH => "Marshall Islands", :MD => "Maryland", :MA => "Massachusetts", :MI => "Michigan", :MN => "Minnesota", :MS => "Mississippi", :MO => "Missouri", :MT => "Montana", :NE => "Nebraska", :NV => "Nevada", :NH => "New Hampshire", :NJ => "New Jersey", :NM => "New Mexico", :NY => "New York", :NC => "North Carolina", :ND => "North Dakota", :MP => "Northern Mariana Islands", :OH => "Ohio", :OK => "Oklahoma", :OR => "Oregon", :PW => "Palau", :PA => "Pennsylvania", :PR => "Puerto Rico", :RI => "Rhode Island", :SC => "South Carolina", :SD => "South Dakota", :TN => "Tennessee", :TX => "Texas", :UT => "Utah", :VT => "Vermont", :VI => "Virgin Islands", :VA => "Virginia", :WA => "Washington", :WV => "West Virginia", :WI => "Wisconsin", :WY => "Wyoming", :AE => "Armed Forces Middle East", :AA => "Armed Forces Americas (except Canada)", :AP => "Armed Forces Pacific"}
states[state_abbr] || state_abbr.to_s
Brilliant! Thanks, Wynn!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Great explanation, thanks Jesse. Changed!!