Tonight I was trying to do some recursive merging of hashes and the only
decent solution I came up with looks like that:
h1 = {:key => {a: 1, b: 2, c: {a: 1, b: 2}}}
h2 = {:key => {c: {a:'a', c: 'c'}, d: 4}}
recursive_merge = lambda{|k, old, new| new.respond_to?(:merge) ?
new.merge(old, &recursive_merge) : new}
puts h1.merge(h2, &recursive_merge)
# => {:key=>{:a=>1, :b=>2, :c=>{:a=>"a", :b=>2, :c=>"c"}, :d=>4}}
http://gist.github.com/724900
(Yes I'm using Ruby 1.9, and if you aren't yet, I'm sorry for you ;) )
The code above seems to be working pretty well but I'm wondering if that's
really the best way to do that, what do you guys think and can anyone come
up with a better solution?
Thanks,
- Matt
--
SD Ruby mailing list
[email protected]
http://groups.google.com/group/sdruby