From: "byroot (Jean Boussier)" <noreply@ruby-lang.org>
To: ruby-core@neon.ruby-lang.org
Subject: [ruby-core:110258] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods
Date: Tue, 11 Oct 2022 18:16:21 +0000 (UTC) [thread overview]
Message-ID: <redmine.journal-99546.20221011181621.24242@ruby-lang.org> (raw)
In-Reply-To: redmine.issue-19047.20221010193052.24242@ruby-lang.org
Issue #19047 has been updated by byroot (Jean Boussier).
> Is there another reason to prefer the alias_method solution?
The main reason to prefer `alias_method` is that it's the least amount of changes, so least likely to break anything.
The fast `super` call argument in favor of using an included module is interesting though. However it means `Overridden.instance_method(:foo).owner` would change. Not that it's a big deal from my point of view though.
----------------------------------------
Bug #19047: DelegateClass displays "method redefined" warning when overriding methods
https://bugs.ruby-lang.org/issues/19047#change-99546
* 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/
next prev parent reply other threads:[~2022-10-11 18:16 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
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 ` byroot (Jean Boussier) [this message]
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)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-list from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.ruby-lang.org/en/community/mailing-lists/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=redmine.journal-99546.20221011181621.24242@ruby-lang.org \
--to=ruby-core@ruby-lang.org \
--cc=ruby-core@neon.ruby-lang.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).