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.2 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.1 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-1.0.0 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-0.9.9.9 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-0.9.9.8.1 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-0.9.9.8 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-0.9.9.7 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-0.9.9.6 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-0.9.9.5 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-0.9.9.4 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-0.9.9.3 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-0.9.9.2 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-0.9.9.1 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-0.9.9 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-0.9.8 lib/riemann/babbler/support/deep_merge.rb
riemann-babbler-0.9.7 lib/deep_merge.rb
riemann-babbler-0.9.6 lib/deep_merge.rb
riemann-babbler-0.9.5 lib/deep_merge.rb
riemann-babbler-0.9.4 lib/deep_merge.rb
riemann-babbler-0.9.3 lib/deep_merge.rb