Skip to content

Commit d5bcab2

Browse files
hsbtclaude
andcommitted
Call Kernel.format explicitly in Gem::Deprecate wrapper
The wrapper method runs in the context of the deprecating class, so a bare format call dispatches to that class's own #format method when it defines one, raising ArgumentError. #9704 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent cefb763 commit d5bcab2

2 files changed

Lines changed: 30 additions & 1 deletion

File tree

lib/rubygems/deprecate.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def deprecate(name, repl, year, month)
110110
msg = [
111111
"NOTE: #{target}#{name} is deprecated",
112112
repl == :none ? " with no replacement" : "; use #{repl} instead",
113-
format(". It will be removed on or after %4d-%02d.", year, month),
113+
Kernel.format(". It will be removed on or after %4d-%02d.", year, month),
114114
"\n#{target}#{name} called from #{Gem.location_of_caller.join(":")}",
115115
]
116116
warn "#{msg.join}." unless Gem::Deprecate.skip

test/rubygems/test_deprecate.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,23 @@ def bar_kwarg(message:)
102102
deprecate :foo_kwarg, :bar_kwarg, 2099, 3
103103
end
104104

105+
class ThingWithFormat
106+
extend Gem::Deprecate
107+
attr_accessor :message
108+
def foo
109+
@message = "foo"
110+
end
111+
112+
def bar
113+
@message = "bar"
114+
end
115+
deprecate :foo, :bar, 2099, 3
116+
117+
def format
118+
raise "ThingWithFormat#format should not be called"
119+
end
120+
end
121+
105122
def test_deprecated_method_calls_the_old_method
106123
capture_output do
107124
thing = Thing.new
@@ -161,4 +178,16 @@ def test_deprecated_method_outputs_a_warning_old_way
161178
assert_match(/OtherThing#foo_kwarg is deprecated; use bar_kwarg instead\./, err)
162179
assert_match(/on or after 2099-03/, err)
163180
end
181+
182+
def test_deprecated_method_when_class_overrides_format
183+
out, err = capture_output do
184+
thing = ThingWithFormat.new
185+
thing.foo
186+
assert_equal "foo", thing.message
187+
end
188+
189+
assert_equal "", out
190+
assert_match(/ThingWithFormat#foo is deprecated; use bar instead\./, err)
191+
assert_match(/on or after 2099-03/, err)
192+
end
164193
end

0 commit comments

Comments
 (0)