Class: Hash

Inherits:
Object
  • Object
show all
Defined in:
lib/core_ext/hash.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#deep_merge(other_hash) ⇒ Object

Non destructive version of deep_merge using a dup

this was called on and the param hash

Parameters:

  • the (Hash)

    hash to be merged with



10
11
12
# File 'lib/core_ext/hash.rb', line 10

def deep_merge(other_hash)
  dup.deep_merge!(other_hash)
end

#deep_merge!(other) ⇒ Object

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

Parameters:

  • the (Hash)

    hash to be merged with

Raises:

  • (ArgumentError)


19
20
21
22
23
24
25
26
27
# 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