Skip to content

Commit a4d7ed1

Browse files
authored
Merge pull request rails#47688 from Shopify/object-with-immediates
Undefine `with` on NilClass and other common immediate types
2 parents 13ea2d7 + dc6bfec commit a4d7ed1

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,9 @@ def with(**attributes)
3636
end
3737
end
3838
end
39+
40+
# #with isn't usable on immediates, so we might as well undefine the
41+
# method in common immediate classes to avoid potential confusion.
42+
[NilClass, TrueClass, FalseClass, Integer, Float].each do |klass|
43+
klass.undef_method(:with)
44+
end

activesupport/test/core_ext/object/with_test.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,12 @@ def initialize
8787
end
8888
assert_equal :mixed, @object.mixed_attr
8989
end
90+
91+
test "basic immediates don't respond to #with" do
92+
assert_not_respond_to nil, :with
93+
assert_not_respond_to true, :with
94+
assert_not_respond_to false, :with
95+
assert_not_respond_to 1, :with
96+
assert_not_respond_to 1.0, :with
97+
end
9098
end

0 commit comments

Comments
 (0)