File tree Expand file tree Collapse file tree 3 files changed +46
-0
lines changed
lib/active_support/core_ext/object Expand file tree Collapse file tree 3 files changed +46
-0
lines changed Original file line number Diff line number Diff line change
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
+
1
19
* Consistently raise an ` ArgumentError` if the ` ActiveSupport::Cache` key is blank.
2
20
3
21
* Joshua Young *
Original file line number Diff line number Diff line change @@ -53,3 +53,19 @@ def deep_dup
53
53
hash
54
54
end
55
55
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
Original file line number Diff line number Diff line change @@ -66,4 +66,16 @@ def test_deep_dup_with_mutable_frozen_key
66
66
67
67
assert_not_equal hash . keys , dup . keys
68
68
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
69
81
end
You can’t perform that action at this time.
0 commit comments