Class: Hash

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

Instance Method Summary collapse

Instance Method Details

#deep_safe_merge(other_hash) ⇒ Object

idea snatched from deep_merge in Rails source code



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/missing_t.rb', line 27

def deep_safe_merge(other_hash)
  self.merge(other_hash) do |key, oldval, newval|
    oldval = oldval.to_hash if oldval.respond_to?(:to_hash)
    newval = newval.to_hash if newval.respond_to?(:to_hash)
    if oldval.is_a? Hash
      if newval.is_a? Hash
        oldval.deep_safe_merge(newval)
      else
        oldval
      end
    else
      newval
    end
  end
end

#deep_safe_merge!(other_hash) ⇒ Object



43
44
45
# File 'lib/missing_t.rb', line 43

def deep_safe_merge!(other_hash)
  replace(deep_safe_merge(other_hash))
end