Ruby/Hash/Hash Elements

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

Версия от 17:10, 26 мая 2010; (Обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

adds the value "Six" with a key 6. Or you can use []= to change a value

myHash = { 1 => "One", 2 => "Two", 3 => "Three", 4 => "Four", 5 => "Five" }
 
myHash[2]= "Bent"



changes the value associated with the key 2 to "Bent".

# you can use the store method to add a pair to the myHash array
myHash = { 1 => "One", 2 => "Two", 3 => "Three", 4 => "Four",
5 => "Five" }
myHash.store(6, "Six")



Changing Hashes

# Hash"s []= method replaces or adds key-value pairs to an existing hash. 
 
myHash = { 1 => "One", 2 => "Two", 3 => "Three", 4 => "Four",5 => "Five" }
# You can use []= to add a pair to this array:
myHash = { 1 => "One", 2 => "Two", 3 => "Three", 4 => "Four", 5 => "Five" }
 
myHash[6]= "Six"