From: "matz (Yukihiro Matsumoto)" Date: 2022-12-01T12:40:02+00:00 Subject: [ruby-core:111127] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods Issue #19047 has been updated by matz (Yukihiro Matsumoto). For example, the example in #19079 does not give warning for overwriting the method `foo`. Matz. ---------------------------------------- Bug #19047: DelegateClass displays "method redefined" warning when overriding methods https://bugs.ruby-lang.org/issues/19047#change-100405 * 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/df2283b8d8874446086b80355c586397f1b48d53/lib/delegate.rb#L442). 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/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/