Ruby/Hash/index
Материал из Wiki.crossplatform.ru
Содержание |
Change values in hash in the same way as an array
dictionary = { "cat" => "feline animal", "dog" => "canine animal" } dictionary["cat"] = "fluffy animal" puts dictionary["cat"]
Get Hash value by index
myHash = { 1 => "One", 2 => "Two", 3 => "Three", 4 => "Four", 5 => "Five" } myHash[6]= "Six" myHash[2]= "Bent"
pulling stuff out of zip.
# Here is a simple way of grabbing a value: the [] method. # It retrieves a single hash value based on a key: zip = { 1 => "One", 2 => "Two", 3 => "Three", 4 => "Four", 5 => "Five", 6 => "Six", 7 => "Seven", 8 => "Eight", 9 => "Eight" } zip[1] # => "One"
return a value for a given key (one key only) with the index method
zip = { 1 => "One", 2 => "Two", 3 => "Three", 4 => "Four", 5 => "Five", 6 => "Six", 7 => "Seven", 8 => "Eight", 9 => "Eight" } zip.index "Three" # => 3
use square brackets to reference the element you wish to retrieve
dictionary = { "cat" => "feline animal", "dog" => "canine animal" } puts dictionary["cat"]