Skip to content

Commit e54ef0a

Browse files
committed
Object#deep_dup no longer duplicate named classes and modules.
It makes very little sense to duplicate named classes and modules. Before: ```ruby hash = { class: Object, module: Kernel } hash.deep_dup # => {:class=>#<Class:0x00000001063ffc80>, :module=>#<Module:0x00000001063ffa00>} ``` After: ```ruby hash = { class: Object, module: Kernel } hash.deep_dup # => {:class=>Object, :module=>Kernel} ```
1 parent b717a5a commit e54ef0a

File tree

3 files changed

+46
-0
lines changed

3 files changed

+46
-0
lines changed

activesupport/CHANGELOG.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,21 @@
1+
* `Object#deep_dup` no longer duplicate named classes and modules.
2+
3+
Before:
4+
5+
```ruby
6+
hash = { class: Object, module: Kernel }
7+
hash.deep_dup # => {:class=>#<Class:0x00000001063ffc80>, :module=>#<Module:0x00000001063ffa00>}
8+
```
9+
10+
After:
11+
12+
```ruby
13+
hash = { class: Object, module: Kernel }
14+
hash.deep_dup # => {:class=>Object, :module=>Kernel}
15+
```
16+
17+
*Jean Boussier*
18+
119
* Consistently raise an `ArgumentError` if the `ActiveSupport::Cache` key is blank.
220

321
*Joshua Young*

activesupport/lib/active_support/core_ext/object/deep_dup.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,19 @@ def deep_dup
5353
hash
5454
end
5555
end
56+
57+
class Module
58+
# Returns a copy of module or class if it's anonymous. If it's
59+
# named, returns +self+.
60+
#
61+
# Object.deep_dup == Object # => true
62+
# klass = Class.new
63+
# klass.deep_dup == klass # => false
64+
def deep_dup
65+
if name.nil?
66+
super
67+
else
68+
self
69+
end
70+
end
71+
end

activesupport/test/core_ext/object/deep_dup_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,16 @@ def test_deep_dup_with_mutable_frozen_key
6666

6767
assert_not_equal hash.keys, dup.keys
6868
end
69+
70+
def test_named_modules_arent_duped
71+
hash = { class: Object, module: Kernel }
72+
assert_equal hash, hash.deep_dup
73+
end
74+
75+
def test_anonymous_modules_are_duped
76+
hash = { class: Class.new, module: Module.new }
77+
duped_hash = hash.deep_dup
78+
assert_not_equal hash[:class], duped_hash[:class]
79+
assert_not_equal hash[:module], duped_hash[:module]
80+
end
6981
end

0 commit comments

Comments
 (0)