v3.2.3 - Show latest stable - 2 notes - Class: Hash
deep_dup() public

Returns a deep copy of hash.

Show source
Register or log in to add new notes.
August 25, 2014 - (v3.1.0 - v3.2.13)
0 thanks

This method does not correctly dup arrays

Watch out because this method does not correctly dup arrays values.

The bug can be reproduced with the following code:

hash = { 'a' => [1,2,3,4] }
dup  = hash.deep_dup
dup['a'].object_id == hash['a'].object_id # should return true

Rails 4 version does not have this issue because it is completely different implementation.

February 2, 2015
0 thanks

Be careful with cycles

This simplistic implementation (unlike Marshal.load(Marshal.dump(object)) doesn’t handle cycles in objects.

a = {}
b = {a: a}
a[:b] = b
a.deep_dup # SystemStackError: stack level too deep