Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Undefine with on NilClass and other common immediate types #47688

Merged
merged 1 commit into from
Mar 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions activesupport/lib/active_support/core_ext/object/with.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ def with(**attributes)
end
end
end

# #with isn't usable on immediates, so we might as well undefine the
# method in common immediate classes to avoid potential confusion.
[NilClass, TrueClass, FalseClass, Integer, Float].each do |klass|
klass.undef_method(:with)
end
8 changes: 8 additions & 0 deletions activesupport/test/core_ext/object/with_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,12 @@ def initialize
end
assert_equal :mixed, @object.mixed_attr
end

test "basic immediates don't respond to #with" do
assert_not_respond_to nil, :with
assert_not_respond_to true, :with
assert_not_respond_to false, :with
assert_not_respond_to 1, :with
assert_not_respond_to 1.0, :with
end
end