Update Code:
irb(main):001:0> h1 = { "a" => 100, "b" => 200 }
=> {"a"=>100, "b"=>200}
irb(main):002:0> h2 = { "b" => 254, "c" => 300 }
=> {"b"=>254, "c"=>300}
irb(main):003:0> h1.update(h2)
=> {"a"=>100, "b"=>254, "c"=>300}
Merge Code:
irb(main):001:0> h1 = { "a" => 100, "b" => 200 }
=> {"a"=>100, "b"=>200}
irb(main):002:0> h2 = { "b" => 254, "c" => 300 }
=> {"b"=>254, "c"=>300}
irb(main):003:0> h1.merge(h2)
=> {"a"=>100, "b"=>254, "c"=>300}
irb(main):004:0>
I ran the above merge and update method on the same hash. But got the same output. So here my question is: are update and merge works with same logic? if not same then how the output came same for those?
No, they are not the same.
updateis an alias formerge!, which is the in-place variant ofmerge.Because in both cases you are using the return value of the call, however, the value of
h1is different in each case: