Skip to content

Commit

Permalink
deep_dup method, remove old key from duplicated hash to avoid unneces…
Browse files Browse the repository at this point in the history
…sary pairs
  • Loading branch information
meinac committed May 4, 2015
1 parent 9619331 commit 40c7f74
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
Expand Up @@ -40,6 +40,7 @@ class Hash
# dup[:a][:c] # => "c"
def deep_dup
each_with_object(dup) do |(key, value), hash|
hash.delete(key)
hash[key.deep_dup] = value.deep_dup
end
end
Expand Down
6 changes: 6 additions & 0 deletions activesupport/test/core_ext/object/deep_dup_test.rb
Expand Up @@ -50,4 +50,10 @@ def test_object_deep_dup
assert dup.instance_variable_defined?(:@a)
end

def test_deep_dup_with_hash_class_key
hash = { Fixnum => 1 }
dup = hash.deep_dup
assert_equal dup.keys.length, 1
end

end

0 comments on commit 40c7f74

Please sign in to comment.