Issue #19047 has been updated by matz (Yukihiro Matsumoto).
Sorry, but by this change, we missed the warnings from DelegateClass misuse. We will
revert this change.
Matz.
----------------------------------------
Bug #19047: DelegateClass displays "method redefined" warning when overriding
methods
https://bugs.ruby-lang.org/issues/19047#change-100373
* Author: jonathanhefner (Jonathan Hefner)
* Status: Closed
* Priority: Normal
* ruby -v: ruby 3.1.2p20
* Backport: 2.7: UNKNOWN, 3.0: UNKNOWN, 3.1: UNKNOWN
----------------------------------------
Perhaps this is not a bug, but it does seem unexpected.
When creating a `DelegateClass` class without an intervening ancestor, overriding a method
displays "method redefined" warning:
```ruby
Base = Class.new do
def foo
"foo"
end
end
Delegate1 = DelegateClass(Base) do
def foo
super + "1"
end
end
# warning: method redefined; discarding old foo
Delegate2 = Class.new(DelegateClass(Base)) do
def foo
super + "2"
end
end
# no warning
Delegate1.new(Base.new).foo
# => "foo1"
Delegate2.new(Base.new).foo
# => "foo2"
```
One possible solution would be to evaluate the `DelegateClass` block in a separate module,
and prepend that module to the returned class.
Another possible solution would be to silence warnings around [when the block is
evaluated](https://github.com/ruby/delegate/blob/df2283b8d8874446086b80355c….
I would be happy to submit a PR to
https://github.com/ruby/delegate if this is something
we want to address.
--
https://bugs.ruby-lang.org/