Accessing Hashes : has_key « Hash « Ruby






Accessing Hashes


Here's a hash that associates zip codes with the names of towns

zip = { 1 => "One", 2 => "Two", 3 => "Three",
4 => "Four", 5 => "Five", 6 => "Six", 7 =>
"Seven", 8 => "Eight", 9 => "Eight" }

# test to see if the hash zip has a given key with any of the following methods, 
# which are all synonyms of each other: key?, has_key?, member?, or include?:

zip.has_key? 1 # => true

 








Related examples in the same category