new

Hash.new â new_hashHash.new(obj) â new_hashHash.new {|hash, key| block } â new_hash Class Public methods 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,

try_convert

Hash.try_convert(obj) â hash or nil Class Public methods 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 Instance Public methods 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 #=>

[] 2

hsh[key] â value Instance Public methods 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 Instance Public methods 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?

assoc

hash.assoc(obj) â an_array or nil Instance Public methods 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

clear

hsh.clear â hsh Instance Public methods Removes all key-value pairs from hsh. h = { "a" => 100, "b" => 200 } #=> {"a"=>100, "b"=>200} h.clear #=> {}

compare_by_identity

hsh.compare_by_identity â hsh Instance Public methods Makes hsh compare its keys by their identity, i.e. it will consider exact same objects as same keys. h1 = { "a" => 100, "b" => 200, :c => "c" } h1["a"] #=> 100 h1.compare_by_identity h1.compare_by_identity? #=> true h1["a"] #=> nil # different objects. h1[:c] #=> "c" # same symbols are all same.

compare_by_identity?

hsh.compare_by_identity? â true or false Instance Public methods Returns true if hsh will compare its keys by their identity. Also see Hash#compare_by_identity.

default

hsh.default(key=nil) â obj Instance Public methods Returns the default value, the value that would be returned by hsh if key did not exist in hsh. See also Hash::new and Hash#default=. h = Hash.new #=> {} h.default #=> nil h.default(2) #=> nil h = Hash.new("cat") #=> {} h.default #=> "cat" h.default(2) #=