module Geocoder::Util
def self.recursive_hash_merge(h1, h2)
h1.merge!(h2) #=> {"a" => 100, "b" = >254, "c" => {"c1" => 16, "c3" => 94}}
Simply using Hash#merge! would return
recursive_hash_merge(h1, h2) #=> {"a" => 100, "b" => 254, "c" => {"c1" => 16, "c2" => 14, "c3" => 94}}
h2 = {"b" => 254, "c" => {"c1" => 16, "c3" => 94}}
h1 = {"a" => 100, "b" => 200, "c" => {"c1" => 12, "c2" => 14}}
it merges and returns the values from both hashes.
When both +h1+ and +h2+ contains an entry with the same key,
Compared with Hash#merge!, this method supports nested hashes.
merging entries in +h1+ with duplicate keys with those from +h2+.
Adds the contents of +h2+ to +h1+,
Recursive version of Hash#merge!
def self.recursive_hash_merge(h1, h2) h1.merge!(h2) do |_key, oldval, newval| oldval.class == h1.class ? self.recursive_hash_merge(oldval, newval) : newval end end