Class Hash Flashcards
Hash[ key, value, … ] → new_hash
Hash[ [ [key, value], … ] ] → new_hash
Hash[ object ] → new_hash
(Public Class Method)
Creates a new hash populated with the given objects. Equivalent to the literal { key => value, … }. In the first form, keys and values occur in pairs, so there must be an even number of arguments. The second and third form take a single argument which is either an array of key-value pairs or an object convertible to a hash.
Hash[“a”, 100, “b”, 200] #=> {“a”=>100, “b”=>200}
Hash[ [ [“a”, 100], [“b”, 200] ] ] #=> {“a”=>100, “b”=>200}
Hash[“a” => 100, “b” => 200] #=> {“a”=>100, “b”=>200}
new → new_hash
new(obj) → new_hash
new {|hash, key| block } → new_hash
(Public Class Method)
Returns a new, empty hash. If this hash is subsequently accessed by a key that doesn’t correspond to a hash entry, the value returned depends on the style of new used to create the hash. In the first form, the access returns nil. If obj is specified, this single object will be used for all default values. If a block is specified, it will be called with the hash object and the key, and should return the default value. It is the block’s responsibility to store the value in the hash if required.
h = Hash.new("Go Fish") h["a"] = 100 h["b"] = 200 h["a"] #=> 100 h["c"] #=> "Go Fish" # The following alters the single default object h["c"].upcase! #=> "GO FISH" h["d"] #=> "GO FISH" h.keys #=> ["a", "b"]
While this creates a new default object each time
h = Hash.new { |hash, key| hash[key] = “Go Fish: #{key}” }
h[“c”] #=> “Go Fish: c”
h[“c”].upcase! #=> “GO FISH: C”
h[“d”] #=> “Go Fish: d”
h.keys #=> [“c”, “d”]
try_convert(obj) → hash or nil
Public Class Method
Try to convert obj into a hash, using #to_hash method. Returns converted hash or nil if obj cannot be converted for any reason.
Hash.try_convert({1=>2}) # => {1=>2}
Hash.try_convert(“1=>2”) # => nil
hsh == other_hash → true or false
Public Instance Method
Equality—Two hashes are equal if they each contain the same number of keys and if each key-value pair is equal to (according to Object#==) the corresponding elements in the other hash.
h1 = { "a" => 1, "c" => 2 } h2 = { 7 => 35, "c" => 2, "a" => 1 } h3 = { "a" => 1, "c" => 2, 7 => 35 } h4 = { "a" => 1, "d" => 2, "f" => 35 } h1 == h2 #=> false h2 == h3 #=> true h3 == h4 #=> false
hsh[key] → value
Public Instance Method
Element Reference—Retrieves the value object corresponding to the key object. If not found, returns the default value (see Hash::new for details).
h = { “a” => 100, “b” => 200 }
h[“a”] #=> 100
h[“c”] #=> nil
hsh[key] = value → value
Public Instance Method
Element Assignment
Associates the value given by value with the key given by key.
h = { “a” => 100, “b” => 200 }
h[“a”] = 9
h[“c”] = 4
h #=> {“a”=>9, “b”=>200, “c”=>4}
key should not have its value changed while it is in use as a key (an unfrozen String passed as a key will be duplicated and frozen).
a = "a" b = "b".freeze h = { a => 100, b => 200 } h.key(100).equal? a #=> false h.key(200).equal? b #=> true
assoc(obj) → an_array or nil
Public Instance Method
Searches through the hash comparing obj with the key using ==. Returns the key-value pair (two elements array) or nil if no match is found. See Array#assoc.
h = {“colors” => [“red”, “blue”, “green”],
“letters” => [“a”, “b”, “c” ]}
h.assoc(“letters”) #=> [“letters”, [“a”, “b”, “c”]]
h.assoc(“foo”) #=> nil