merge(*args) public

Returns a new hash that combines the contents of the receiver and the contents of the given hashes.

If no block is given, entries with duplicate keys are overwritten with the values from each other_hash successively, otherwise the value for each duplicate key is determined by calling the block with the key, its value in the receiver and its value in each other_hash.

When called without any argument, returns a copy of the receiver.

h1 = { "a" => 100, "b" => 200 }
h2 = { "b" => 246, "c" => 300 }
h3 = { "b" => 357, "d" => 400 }
h1.merge          #=> {"a"=>100, "b"=>200}
h1.merge(h2)      #=> {"a"=>100, "b"=>246, "c"=>300}
h1.merge(h2, h3)  #=> {"a"=>100, "b"=>357, "c"=>300, "d"=>400}
h1.merge(h2) {|key, oldval, newval| newval - oldval}
                  #=> {"a"=>100, "b"=>46,  "c"=>300}
h1.merge(h2, h3) {|key, oldval, newval| newval - oldval}
                  #=> {"a"=>100, "b"=>311, "c"=>300, "d"=>400}
h1                #=> {"a"=>100, "b"=>200}
Show source
Register or log in to add new notes.