Ruby/Hash/has key

Материал из Wiki.crossplatform.ru

(Различия между версиями)
Перейти к: навигация, поиск
м (1 версия: Импорт выборки материалов по Ruby)
 

Текущая версия на 17:57, 13 сентября 2010

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