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 |
Great explanation, thanks Jesse. Changed!!
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
It makes the lookup in the hash faster because a symbol is cached when it is created. Since that's the case it doesn't have to do a string comparison. The time for a symbol comparison is O(1) vs a string which is O(n) because the symbol is given a unqiue object_id.
You can test it out by doing :AK.object_id == :AK.object_id in irb. If you do "AK".object_id == "AK".object_id you'll notice it is false meaning that the same string is actually two different instances.
From what I understand the conversion from string to to symbol is a pretty small overhead and the benefits that you get from referencing a symbol as a key in your hash more than makes up for the conversion because it can hash a symbol and perform better than a string.