class Hash

Hash Patches

Public Instance Methods

deep_merge(second) click to toggle source

Merges self with another second, recursively.

This code was lovingly stolen from some random gem: gemjack.com/gems/tartan-0.1.1/classes/Hash.html

Thanks to whoever made it.

Modified to provide same functionality with Arrays

# File lib/rest_connection/patches.rb, line 36
def deep_merge(second)
  target = dup
  return target unless second
  unless Hash === second
    raise TypeError.new("can't convert #{second.class} into #{self.class}")
  end
  second.keys.each do |k|
    if second[k].is_a? Array and self[k].is_a? Array
      target[k] = target[k].deep_merge(second[k])
      next
    elsif second[k].is_a? Hash and self[k].is_a? Hash
      target[k] = target[k].deep_merge(second[k])
      next
    end
    target[k] = second[k]
  end
  target
end
deep_merge!(second) click to toggle source

From: www.gemtacular.com/gemdocs/cerberus-0.2.2/doc/classes/Hash.html File lib/cerberus/utils.rb, line 42 Modified to provide same functionality with Arrays

# File lib/rest_connection/patches.rb, line 59
def deep_merge!(second)
  return nil unless second
  unless Hash === second
    raise TypeError.new("can't convert #{second.class} into #{self.class}")
  end
  second.each_pair do |k,v|
    if self[k].is_a?(Array) and second[k].is_a?(Array)
      self[k].deep_merge!(second[k])
    elsif self[k].is_a?(Hash) and second[k].is_a?(Hash)
      self[k].deep_merge!(second[k])
    else
      self[k] = second[k]
    end
  end
end