Sha256: 8609d89fdaac0044e4b44e7640721c683df706051a21952a953e228fa8418601

Contents?: true

Size: 1 KB

Versions: 14

Compression:

Stored size: 1 KB

Contents

# frozen_string_literal: true

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

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
geocoder-1.8.3 lib/geocoder/util.rb
geocoder-1.8.2 lib/geocoder/util.rb
geocoder-1.8.1 lib/geocoder/util.rb
geocoder-1.8.0 lib/geocoder/util.rb
geocoder-1.7.5 lib/geocoder/util.rb
geocoder-1.7.4 lib/geocoder/util.rb
geocoder-1.7.3 lib/geocoder/util.rb
geocoder-1.7.2 lib/geocoder/util.rb
geocoder-1.7.1 lib/geocoder/util.rb
geocoder-1.7.0 lib/geocoder/util.rb
geocoder-1.6.7 lib/geocoder/util.rb
geocoder-1.6.6 lib/geocoder/util.rb
geocoder-1.6.5 lib/geocoder/util.rb
geocoder-1.6.4 lib/geocoder/util.rb