Sha256: 050226d9af3a4c5a824580b126cdca4fde9187b45b92b040ec99749733781b21

Contents?: true

Size: 1.36 KB

Versions: 84

Compression:

Stored size: 1.36 KB

Contents

# https://raw.github.com/Offirmo/hash-deep-merge/master/lib/hash_deep_merge.rb
class Hash

  def deep_merge!(specialized_hash)
    internal_deep_merge!(self, specialized_hash)
  end


  def deep_merge(specialized_hash)
    internal_deep_merge!(Hash.new.replace(self), specialized_hash)
  end


  protected

  # better, recursive, preserving method
  # OK OK this is not the most efficient algorithm,
  # but at last it's *perfectly clear and understandable*
  # so fork and improve if you need 5% more speed, ok ?
  def internal_deep_merge!(source_hash, specialized_hash)

    #puts "starting deep merge..."

    specialized_hash.each_pair do |rkey, rval|
      #puts "   potential replacing entry : " + rkey.inspect

      if source_hash.has_key?(rkey) then
        #puts "   found potentially conflicting entry for #{rkey.inspect} : #{rval.inspect}, will merge :"
        if rval.is_a?(Hash) and source_hash[rkey].is_a?(Hash) then
          #puts "      recursing..."
          internal_deep_merge!(source_hash[rkey], rval)
        elsif rval == source_hash[rkey] then
          #puts "      same value, skipping."
        else
          #puts "      replacing."
          source_hash[rkey] = rval
        end
      else
        #puts "   found new entry #{rkey.inspect}, adding it..."
        source_hash[rkey] = rval
      end
    end

    #puts "deep merge done."

    source_hash
  end
end

Version data entries

84 entries across 84 versions & 1 rubygems

Version Path
riemann-babbler-1.0.7.8 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.7.7 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.7.5 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.7.4 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.1.0pre2 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.7.3 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.7.2 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.7.1 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.7 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.6 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.5.7 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.5.6 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.5.5 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.5.4 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.5.3 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.5.2 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.5.1 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.5 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.4 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.3 lib/riemann/babbler/support/deep_merge.rb