From: Hammed Malik Date: 2006-08-14T01:09:26+09:00 Subject: Re: How to update nested Hashes? ------=_Part_140427_6831894.1155485363623 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Content-Disposition: inline Thanks David! On 8/14/06, dblack@wobblini.net wrote: > > Hi -- > > On Mon, 14 Aug 2006, Hammed Malik 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 > > -- > http://www.rubypowerandlight.com => Ruby/Rails training & consultancy > ----> SEE SPECIAL DEAL FOR RUBY/RAILS USERS GROUPS! <----- > http://dablog.rubypal.com => D[avid ]A[. ]B[lack's][ Web]log > http://www.manning.com/black => book, Ruby for Rails > http://www.rubycentral.org => Ruby Central, Inc. > > ------=_Part_140427_6831894.1155485363623--