Class: Hash

Inherits:
Object show all
Defined in:
lib/active_support_ext/object/deep_dup.rb

Direct Known Subclasses

Grover::OptionsBuilder

Instance Method Summary collapse

Instance Method Details

#deep_dupObject

Returns a deep copy of hash.

hash = { a: { b: 'b' } }
dup  = hash.deep_dup
dup[:a][:c] = 'c'

hash[:a][:c] # => nil
dup[:a][:c]  # => "c"


46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/active_support_ext/object/deep_dup.rb', line 46

def deep_dup
  hash = dup
  each_pair do |key, value|
    if key.frozen? && key.is_a?(::String)
      hash[key] = value.deep_dup
    else
      hash.delete(key)
      hash[key.deep_dup] = value.deep_dup
    end
  end
  hash
end