Hash recursive merge in Ruby

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"}}



  1. dubek

    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 (like merge).

    Thanks,
    dubek.

  2. dubek

    I meant like merge! (of course).

  3. James M Long

    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.

Leave a Comment




HK Web & Tech Meetup Index

HK Ruby on Rails User Group

My Tumblr blog

Categories



    Friends blog


    Read Write Sleep
    雨樂文康匯
    852 Signal
    Enjoy Rails
    Gang Lu
    Web Wednesday

    RoRCraft

    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.


    View Rex Chung's profile on LinkedIn

    www.flickr.com

    LATEST RUBY JOBS

    Embed this on your site?