For multi-dimension Hash, .merge will replace the values as a 2 dimension hash.
>> {"number" => {"1" => "one","2" => "two"}}.merge({"number" => {"3" => "three"}})
=> {"number"=>{"3"=>"three"}}
We needed the keep the hash values and merge complex hash together that is loaded from YAML.
Solution?
class Hash
def recursive_merge(h)
self.merge!(h) {|key, _old, _new| if _old.class == Hash then _old.recursive_merge(_new) else _new end }
end
end
>>{"number" => {"1" => "one","2" => "two"}}.recursive_merge({"number" => {"3" => "three"}})
=> {"number"=>{"1"=>"one", "2"=>"two", "3"=>"three"}}
Latest Entries
Archives
- November 2008
- September 2008
- August 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- November 2007
- July 2007
- June 2007
- April 2007
- March 2007
- February 2007
- January 2007
- December 2006
- November 2006
- October 2006
- September 2006
- August 2006
- July 2006
- June 2006
- May 2006
- April 2006
- February 2006
- January 2006
- December 2005
Categories
- ankoder (1)
- australia (2)
- business (13)
- china (4)
- conference (1)
- environment (2)
- facebook (1)
- firefox (1)
- git (2)
- github (2)
- hong kong (1)
- interface (1)
- internet (1)
- javascript (1)
- jobboard (1)
- olympics (1)
- osx (1)
- personal (14)
- politics (5)
- railscamp (1)
- rorcraft (4)
- ruby on rails (4)
- scriptaculous (1)
- startup (2)
- sydney (1)
- taiwan (1)
- travel (3)
- trend (1)
- trends (1)
- twitter (1)
- Uncategorized (9)
- unfuddle (1)
- usability (1)
Twitter Updates
I am the founder of RoRCraft Ltd., a web consultancy firm that develops usable and speedy web applications for our clients.
Based in Sydney, I travel to Hong Kong and HangZhou China to meet with my team regularly. I love exploring new technologies and business ideas that helps making our world a better place.

An online video conversion tool. It allows everyone to freely convert their videos for ipods, mobiles and flash.
Rails Job
Job board for Ruby and Rails developers.

Mar 28th, 2007 at 1:16 am
Great! I just used it in my own app for the exact reason you mentioned (mixing non-flat configurations from two sources).
I suggest renaming the method name to
recursive_merge!to denote that it is destructive (likemerge).Thanks,
dubek.
Mar 28th, 2007 at 1:17 am
I meant like
merge!(of course).Apr 5th, 2007 at 9:40 am
I am glad to see that recursive_merge method, it is awesome and works like I thought merge would. I went looking earlier today and lo and behold… your code showed up. I can’t believe it isn’t part of the Hash class.
Now I can finish one of my pet projects. Thank you.