Method: Hash#deep_dup

Defined in:
lib/sc-core-ext/hash.rb

#deep_dupObject

Performs a “deep copy” of this hash; that is, returns a Hash that is a duplicate of this Hash, and whose keys and values have each, in turn, had #deep_dup or #dup called on them. This should produce a Hash whose every element is a copy of the original.

This operation is expensive, and should be used sparingly.



25
26
27
28
29
30
31
32
# File 'lib/sc-core-ext/hash.rb', line 25

def deep_dup
  inject(self.class.new) do |new_hash, (key, value)|
    key = key.respond_to?(:deep_dup) ? key.deep_dup : key.dup?
    value = value.respond_to?(:deep_dup) ? value.deep_dup : value.dup?
    new_hash[key] = value
    new_hash
  end
end