default_proc

hsh.default_proc â anObject Instance Public methods If Hash::new was invoked with a block, return that block, otherwise return nil. h = Hash.new {|h,k| h[k] = k*k } #=> {} p = h.default_proc #=> #<Proc:0x401b3d08@-:1> a = [] #=> [] p.call(a, 2) a #=> [nil, nil, 4]

default=

hsh.default = obj â obj Instance Public methods Sets the default value, the value returned for a key that does not exist in the hash. It is not possible to set the default to a Proc that will be executed on each key lookup. h = { "a" => 100, "b" => 200 } h.default = "Go fish" h["a"] #=> 100 h["z"] #=> "Go fish" # This doesn't do what you might hope... h.default = proc do |hash, key| hash[key] = key + key end h[2] #=> #<Proc:0x401b3948@-:6>

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) #=

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.

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.

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 #=> {}

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

[]=

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?

[] 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 == 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 #=>