class Hash

The core Hash class This adds the deep_merge method to this core class which is used to join nested hashes

Public Instance Methods

deep_merge(other_hash) click to toggle source

Non destructive version of deep_merge using a dup

@param [Hash] the hash to be merged with @returns [Hash] A copy of a new hash merging the hash this was called on and the param hash

# File lib/core_ext/hash.rb, line 10
def deep_merge(other_hash)
  dup.deep_merge!(other_hash)
end
deep_merge!(other) click to toggle source

Recusively merges hashes into each other. Any value that is not a Hash will be overridden with the value in the other hash.

@param [Hash] the hash to be merged with @returns [Hash] A copy of itself with the new hash merged in

# File lib/core_ext/hash.rb, line 19
def deep_merge!(other)
  raise ArgumentError unless other.is_a?(Hash)

  other.each do |k, v|
    self[k] = (self[k].is_a?(Hash) && self[k].is_a?(Hash)) ? self[k].deep_merge(v) : v
  end

  self
end