ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:110250] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods
@ 2022-10-10 19:30 jonathanhefner (Jonathan Hefner)
  2022-10-11  1:55 ` [ruby-core:110252] " nobu (Nobuyoshi Nakada)
                   ` (12 more replies)
  0 siblings, 13 replies; 14+ messages in thread
From: jonathanhefner (Jonathan Hefner) @ 2022-10-10 19:30 UTC (permalink / raw)
  To: ruby-core

Issue #19047 has been reported by jonathanhefner (Jonathan Hefner).

----------------------------------------
Bug #19047: DelegateClass displays "method redefined" warning when overriding methods
https://bugs.ruby-lang.org/issues/19047

* Author: jonathanhefner (Jonathan Hefner)
* Status: Open
* 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/

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2022-12-01 12:40 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-10 19:30 [ruby-core:110250] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods jonathanhefner (Jonathan Hefner)
2022-10-11  1:55 ` [ruby-core:110252] " nobu (Nobuyoshi Nakada)
2022-10-11 12:36 ` [ruby-core:110254] " byroot (Jean Boussier)
2022-10-11 18:09 ` [ruby-core:110257] " jonathanhefner (Jonathan Hefner)
2022-10-11 18:16 ` [ruby-core:110258] " byroot (Jean Boussier)
2022-10-11 20:50 ` [ruby-core:110260] " jonathanhefner (Jonathan Hefner)
2022-10-11 20:52 ` [ruby-core:110261] " byroot (Jean Boussier)
2022-10-11 21:41 ` [ruby-core:110262] " jonathanhefner (Jonathan Hefner)
2022-10-12  5:08 ` [ruby-core:110268] " byroot (Jean Boussier)
2022-10-13 16:36 ` [ruby-core:110278] " jonathanhefner (Jonathan Hefner)
2022-10-17  1:12 ` [ruby-core:110326] " hsbt (Hiroshi SHIBATA)
2022-12-01  5:15 ` [ruby-core:111105] " matz (Yukihiro Matsumoto)
2022-12-01  8:52 ` [ruby-core:111123] " byroot (Jean Boussier)
2022-12-01 12:40 ` [ruby-core:111127] " matz (Yukihiro Matsumoto)

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).