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_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_proc=

hsh.default_proc = proc_obj or nil Instance Public methods Sets the default proc to be executed on each failed key lookup. h.default_proc = proc do |hash, key| hash[key] = key + key end h[2] #=> 4 h["cat"] #=> "catcat"

delete

hsh.delete(key) â valuehsh.delete(key) {| key | block } â value Instance Public methods Deletes the key-value pair and returns the value from hsh whose key is equal to key. If the key is not found, returns the default value. If the optional code block is given and the key is not found, pass in the key and return the result of block. h = { "a" => 100, "b" => 200 } h.delete("a") #=> 100 h.delete("z")

delete_if

hsh.delete_if {| key, value | block } â hshhsh.delete_if â an_enumerator Instance Public methods Deletes every key-value pair from hsh for which block evaluates to true. If no block is given, an enumerator is returned instead. h = { "a" => 100, "b" => 200, "c" => 300 } h.delete_if {|key, value| key >= "b" } #=> {"a"=>100}

each

hsh.each {| key, value | block } â hshhsh.each_pair {| key, value | block } â hshhsh.each â an_enumeratorhsh.each_pair â an_enumerator Instance Public methods Calls block once for each key in hsh, passing the key-value pair as parameters. If no block is given, an enumerator is returned instead. h = { "a" => 100, "b" => 200 } h.each {|key, value| puts "#{key} is #{value}" } produces: a is 100 b is 200

each_key

hsh.each_key {| key | block } â hshhsh.each_key â an_enumerator Instance Public methods Calls block once for each key in hsh, passing the key as a parameter. If no block is given, an enumerator is returned instead. h = { "a" => 100, "b" => 200 } h.each_key {|key| puts key } produces: a b

each_pair

hsh.each_pair {| key, value | block } â hshhsh.each_pair â an_enumerator Instance Public methods Calls block once for each key in hsh, passing the key-value pair as parameters. If no block is given, an enumerator is returned instead. h = { "a" => 100, "b" => 200 } h.each {|key, value| puts "#{key} is #{value}" } produces: a is 100 b is 200

each_value

hsh.each_value {| value | block } â hshhsh.each_value â an_enumerator Instance Public methods Calls block once for each key in hsh, passing the value as a parameter. If no block is given, an enumerator is returned instead. h = { "a" => 100, "b" => 200 } h.each_value {|value| puts value } produces: 100 200

empty?

hsh.empty? â true or false Instance Public methods Returns true if hsh contains no key-value pairs. {}.empty? #=> true