How to update nested Hashes?

Given two nested Hashes, is there an easy way to update one with the
other?

a = {:top => {:foo => {:a => 1}, :bar => {:b => 2}}}
b = {:top => {:bar => {:c => 3}}}

what I’d like is

a.nested_update(b) => {:top => {:foo => {:a => 1}, :bar => {:c => 3}}}

Thanks

Hammed

Hi –

On Mon, 14 Aug 2006, Hammed M. wrote:

Given two nested Hashes, is there an easy way to update one with the other?

a = {:top => {:foo => {:a => 1}, :bar => {:b => 2}}}
b = {:top => {:bar => {:c => 3}}}

what I’d like is

a.nested_update(b) => {:top => {:foo => {:a => 1}, :bar => {:c => 3}}}

You could do something like:

def a.nested_update(b)
each_key {|k| self[k].update(b[k])}
end

David

Thanks David!