Skip to content

Commit 472e372

Browse files
committed
Deprecate Kernel#with in favor of instance_eval
Kernel#with(obj) { ... } is just a more readable spelling of obj.instance_eval { ... }. Rails 7.1 added an unrelated Object#with that temporarily sets attributes, and whichever loads last wins, so Facets' version silently broke Rails' when both were loaded: with(name: "sue") evaluated the block against the hash instead of setting name. It now warns and is scheduled for removal after 2027-09-30, like the other deprecations. The docs also claimed it worked like `returning`, but it returns the block's value; they now say so. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E2Njj4j1r9twkVcXsMySLW
1 parent 1335864 commit 472e372

2 files changed

Lines changed: 15 additions & 9 deletions

File tree

‎HISTORY.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ Changes:
2323
`Enumerable#uniq_by`, `Kernel#silence_stream` and Facets' fallback
2424
`Time#to_time` are scheduled for removal after 2027-09-30. Each warns
2525
with its replacement and the removal date.
26+
* `Kernel#with` is deprecated in favor of `instance_eval`, also for removal
27+
after 2027-09-30. Rails 7.1's unrelated `Object#with` (which temporarily
28+
sets attributes) was silently replaced by it when both were loaded.
2629
* `Kernel#silence` now does the stream silencing itself; `silently`,
2730
`silence_stderr` and `silence_stdout` use it and do not warn.
2831

‎lib/core/facets/kernel/with.rb‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
module Kernel
22

3-
# Like returning but exectues the block
4-
# via instance_eval.
3+
# Evaluate the block with +obj+ as self and return the block's value.
4+
# It is simply a more readable spelling of #instance_eval.
55
#
6-
# def foo
7-
# with values = [] do
8-
# self << 'bar'
9-
# self << 'baz'
10-
# end
6+
# values = []
7+
# with values do
8+
# self << 'bar'
9+
# self << 'baz'
1110
# end
11+
# values # => ['bar', 'baz']
1212
#
13-
# foo # => ['bar', 'baz']
13+
# @deprecated Use obj.instance_eval { ... } instead. Rails 7.1 defines an
14+
# unrelated Object#with that temporarily sets attributes, and this one
15+
# silently replaces it. Scheduled for removal after 2027-09-30.
1416
#
1517
def with(obj=self, &block)
18+
warn "Kernel#with is deprecated. Use obj.instance_eval { ... } instead. " \
19+
"It will be removed after 2027-09-30.", uplevel: 1
1620
obj.instance_eval(&block)
1721
end
1822

1923
end
20-

0 commit comments

Comments
 (0)