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