Hash#update - need tweaks

I am in a situation,where I have to the below kind of operations.

h1 = {:url_id => 1, :used => 10}
h2 = {:url_id => 1, :used => 5}
h1.update(h2) {|k,old,new| old + new }
# => {:url_id=>2, :used=>15}

now the above code is working as it supposed to do. Now I actually want
the hash output `{:url_id=>1, :used=>15}`.

But using `Hash#update` can this be achieved? any tweak to my code?

···

--
Posted via http://www.ruby-forum.com/.

I finally reach out to my goal,which is

h1 = {:url_id => 1, :used => 10}
h2 = {:url_id => 1, :used => 5}
h3 = h1.update(h2) do |k,old,new|
if k == :used
  old+new
else
  old
end
end
h3
# => {:url_id=>1, :used=>15}

···

--
Posted via http://www.ruby-forum.com/.

What's wrong with

h2.each { |k,v| h1[k] += v }

···

--
Posted via http://www.ruby-forum.com/.

Joel Pearson wrote in post #1116045:

What's wrong with

h2.each { |k,v| h1[k] += v }

I do have array of hashes,on which I need to perform this. Thus I
thought about `Hash#update`.

···

--
Posted via http://www.ruby-forum.com/\.