Sha256: 089acdf4b68e93e37f4e6f8b75f72a4ec670603f022a483e1cf41f8c5b90a44d
Contents?: true
Size: 884 Bytes
Versions: 9
Compression:
Stored size: 884 Bytes
Contents
module Foreplay class Utility # Returns a new hash with +hash+ and +other_hash+ merged recursively, including arrays. # # h1 = { x: { y: [4,5,6] }, z: [7,8,9] } # h2 = { x: { y: [7,8,9] }, z: 'xyz' } # h1.supermerge(h2) # #=> {:x=>{:y=>[4, 5, 6, 7, 8, 9]}, :z=>[7, 8, 9, "xyz"]} def self.supermerge(hash, other_hash) fail 'supermerge only works if you pass two hashes' unless hash.is_a?(Hash) && other_hash.is_a?(Hash) new_hash = hash.deep_dup.with_indifferent_access other_hash.each_pair do |k, v| tv = new_hash[k] if tv.is_a?(Hash) && v.is_a?(Hash) new_hash[k] = Foreplay::Utility.supermerge(tv, v) elsif tv.is_a?(Array) || v.is_a?(Array) new_hash[k] = Array.wrap(tv) + Array.wrap(v) else new_hash[k] = v end end new_hash end end end
Version data entries
9 entries across 9 versions & 1 rubygems