-
-
Save jmccartie/733658 to your computer and use it in GitHub Desktop.
# 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 |
I'm passing strings to this function, so is the "to_sym" conversion still faster than using strings in the method?
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.
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!
I would use symbols for the keys because symbols don't get recreated like strings and they are easier for comparison in Ruby.
I.e.
states = {:AK => "Alaska", :AL => "Alabama", ....
You can still accept strings by doing state_abbr.to_sym.