* [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
* [ruby-core:110252] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods
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 ` nobu (Nobuyoshi Nakada)
2022-10-11 12:36 ` [ruby-core:110254] " byroot (Jean Boussier)
` (11 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: nobu (Nobuyoshi Nakada) @ 2022-10-11 1:55 UTC (permalink / raw)
To: ruby-core
Issue #19047 has been updated by nobu (Nobuyoshi Nakada).
Yet another possible solution would to add `alias_method(method, method)` after `define_method` calls.
----------------------------------------
Bug #19047: DelegateClass displays "method redefined" warning when overriding methods
https://bugs.ruby-lang.org/issues/19047#change-99541
* 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
* [ruby-core:110254] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods
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 ` byroot (Jean Boussier)
2022-10-11 18:09 ` [ruby-core:110257] " jonathanhefner (Jonathan Hefner)
` (10 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: byroot (Jean Boussier) @ 2022-10-11 12:36 UTC (permalink / raw)
To: ruby-core
Issue #19047 has been updated by byroot (Jean Boussier).
For context this was originally discussed on a Rails PR to eliminate warnings from our test suite: https://github.com/rails/rails/pull/46189#discussion_r987395361
In my opinion this is a bug, because if you are creating a delegator, it's very likely you'll redefine at least some of the delegated methods.
The `alias_method` trick is likely the way to go.
----------------------------------------
Bug #19047: DelegateClass displays "method redefined" warning when overriding methods
https://bugs.ruby-lang.org/issues/19047#change-99543
* 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
* [ruby-core:110257] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods
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 ` jonathanhefner (Jonathan Hefner)
2022-10-11 18:16 ` [ruby-core:110258] " byroot (Jean Boussier)
` (9 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: jonathanhefner (Jonathan Hefner) @ 2022-10-11 18:09 UTC (permalink / raw)
To: ruby-core
Issue #19047 has been updated by jonathanhefner (Jonathan Hefner).
Indeed, `alias_method` is another possibility! And also [Jean's suggestion](https://github.com/rails/rails/pull/46189#discussion_r988685084) of defining the delegator methods in a module.
Unfortunately, my suggestion of evaluating the block in a module does not work for metaprogramming within the block, such as [in this test](https://github.com/ruby/delegate/blob/df2283b8d8874446086b80355c586397f1b48d53/test/test_delegate.rb#L27).
I had not really dug into `DelegateClass` to understand why the `super` in `Delegate1` still works after redefining `foo`. I see now that it's because the class [is a subclass of `Delegator`](https://github.com/ruby/delegate/blob/df2283b8d8874446086b80355c586397f1b48d53/lib/delegate.rb#L395), which also [responds to `foo` via `method_missing`](https://github.com/ruby/delegate/blob/df2283b8d8874446086b80355c586397f1b48d53/lib/delegate.rb#L82-L93). Therefore, redefining `foo` causes `super` to fall back to a slower execution path.
So I benchmarked the solutions with this script:
```ruby
# frozen_string_literal: true
require "benchmark/ips"
$LOAD_PATH.prepend(".../delegate/lib")
require "delegate"
Base = Class.new { def foo; end }
Overridden = DelegateClass(Base) { def foo; super; end }
overridden = Overridden.new(Base.new)
Benchmark.ips do |x|
x.report("overridden") { overridden.foo }
end
```
With this `alias_method` patch:
```diff
index 70d4e4a..af95c86 100644
--- a/lib/delegate.rb
+++ b/lib/delegate.rb
@@ -412,10 +412,12 @@ def DelegateClass(superclass, &block)
end
protected_instance_methods.each do |method|
define_method(method, Delegator.delegating_block(method))
+ alias_method(method, method)
protected method
end
public_instance_methods.each do |method|
define_method(method, Delegator.delegating_block(method))
+ alias_method(method, method)
end
end
klass.define_singleton_method :public_instance_methods do |all=true|
```
the result is:
```
Warming up --------------------------------------
overridden 76.674k i/100ms
Calculating -------------------------------------
overridden 765.489k (± 0.9%) i/s - 3.834M in 5.008559s
```
With this delegator methods module patch:
```diff
index 70d4e4a..4311bb5 100644
--- a/lib/delegate.rb
+++ b/lib/delegate.rb
@@ -410,6 +410,8 @@ def DelegateClass(superclass, &block)
__raise__ ::ArgumentError, "cannot delegate to self" if self.equal?(obj)
@delegate_dc_obj = obj
end
+ end
+ klass.include(Module.new do
protected_instance_methods.each do |method|
define_method(method, Delegator.delegating_block(method))
protected method
@@ -417,7 +419,7 @@ def DelegateClass(superclass, &block)
public_instance_methods.each do |method|
define_method(method, Delegator.delegating_block(method))
end
- end
+ end)
klass.define_singleton_method :public_instance_methods do |all=true|
super(all) | superclass.public_instance_methods
end
```
the result is:
```
Warming up --------------------------------------
overridden 183.938k i/100ms
Calculating -------------------------------------
overridden 1.830M (± 0.9%) i/s - 9.197M in 5.026683s
```
Comparison: the `alias_method` solution is 2.39x slower than the delegator methods module solution.
Is there another reason to prefer the `alias_method` solution?
----------------------------------------
Bug #19047: DelegateClass displays "method redefined" warning when overriding methods
https://bugs.ruby-lang.org/issues/19047#change-99545
* 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 related [flat|nested] 14+ messages in thread
* [ruby-core:110258] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods
2022-10-10 19:30 [ruby-core:110250] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods jonathanhefner (Jonathan Hefner)
` (2 preceding siblings ...)
2022-10-11 18:09 ` [ruby-core:110257] " jonathanhefner (Jonathan Hefner)
@ 2022-10-11 18:16 ` byroot (Jean Boussier)
2022-10-11 20:50 ` [ruby-core:110260] " jonathanhefner (Jonathan Hefner)
` (8 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: byroot (Jean Boussier) @ 2022-10-11 18:16 UTC (permalink / raw)
To: ruby-core
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/
^ permalink raw reply [flat|nested] 14+ messages in thread
* [ruby-core:110260] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods
2022-10-10 19:30 [ruby-core:110250] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods jonathanhefner (Jonathan Hefner)
` (3 preceding siblings ...)
2022-10-11 18:16 ` [ruby-core:110258] " byroot (Jean Boussier)
@ 2022-10-11 20:50 ` jonathanhefner (Jonathan Hefner)
2022-10-11 20:52 ` [ruby-core:110261] " byroot (Jean Boussier)
` (7 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: jonathanhefner (Jonathan Hefner) @ 2022-10-11 20:50 UTC (permalink / raw)
To: ruby-core
Issue #19047 has been updated by jonathanhefner (Jonathan Hefner).
> The main reason to prefer `alias_method` is that it's the least amount of changes, so least likely to break anything.
That's a good reason! 😄
There is one more use case that would be affected:
```ruby
Base = Class.new do
def foo
"foo"
end
end
Helper = Module.new do
def foo
super + "!"
end
end
WithHelper = DelegateClass(Base) { include Helper }
WithHelper.new(Base.new).foo
# BEFORE:
# => "foo"
#
# AFTER:
# => "foo!"
```
In my opinion, the "BEFORE" is surprising, and the "AFTER" is more desirable, but I understand not wanting to break existing code.
If it would be better, I can submit a PR with the `alias_method` patch, and then open a new issue for this use case (with the "delegator methods module" patch attached).
----------------------------------------
Bug #19047: DelegateClass displays "method redefined" warning when overriding methods
https://bugs.ruby-lang.org/issues/19047#change-99548
* 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
* [ruby-core:110261] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods
2022-10-10 19:30 [ruby-core:110250] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods jonathanhefner (Jonathan Hefner)
` (4 preceding siblings ...)
2022-10-11 20:50 ` [ruby-core:110260] " jonathanhefner (Jonathan Hefner)
@ 2022-10-11 20:52 ` byroot (Jean Boussier)
2022-10-11 21:41 ` [ruby-core:110262] " jonathanhefner (Jonathan Hefner)
` (6 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: byroot (Jean Boussier) @ 2022-10-11 20:52 UTC (permalink / raw)
To: ruby-core
Issue #19047 has been updated by byroot (Jean Boussier).
> I can submit a PR with the alias_method patch, and then open a new issue for this use case (with the "delegator methods module" patch attached).
Sounds like the correct approach to me. First a patch with basically no change which is super easy to merge, then an optimization with potential backward compatibility, that may or may not be merged.
----------------------------------------
Bug #19047: DelegateClass displays "method redefined" warning when overriding methods
https://bugs.ruby-lang.org/issues/19047#change-99549
* 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
* [ruby-core:110262] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods
2022-10-10 19:30 [ruby-core:110250] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods jonathanhefner (Jonathan Hefner)
` (5 preceding siblings ...)
2022-10-11 20:52 ` [ruby-core:110261] " byroot (Jean Boussier)
@ 2022-10-11 21:41 ` jonathanhefner (Jonathan Hefner)
2022-10-12 5:08 ` [ruby-core:110268] " byroot (Jean Boussier)
` (5 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: jonathanhefner (Jonathan Hefner) @ 2022-10-11 21:41 UTC (permalink / raw)
To: ruby-core
Issue #19047 has been updated by jonathanhefner (Jonathan Hefner).
Submitted `alias_method` patch as https://github.com/ruby/delegate/pull/13.
----------------------------------------
Bug #19047: DelegateClass displays "method redefined" warning when overriding methods
https://bugs.ruby-lang.org/issues/19047#change-99550
* 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
* [ruby-core:110268] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods
2022-10-10 19:30 [ruby-core:110250] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods jonathanhefner (Jonathan Hefner)
` (6 preceding siblings ...)
2022-10-11 21:41 ` [ruby-core:110262] " jonathanhefner (Jonathan Hefner)
@ 2022-10-12 5:08 ` byroot (Jean Boussier)
2022-10-13 16:36 ` [ruby-core:110278] " jonathanhefner (Jonathan Hefner)
` (4 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: byroot (Jean Boussier) @ 2022-10-12 5:08 UTC (permalink / raw)
To: ruby-core
Issue #19047 has been updated by byroot (Jean Boussier).
Thank you, now the issue is that `delegate` doesn't have a maintainer: https://github.com/ruby/ruby/commit/1159dbf305603b60a1e5d2b9ff77a9cf30775296
I'll be unavailable for the rest of the week, but I'll try to ask around next week.
----------------------------------------
Bug #19047: DelegateClass displays "method redefined" warning when overriding methods
https://bugs.ruby-lang.org/issues/19047#change-99554
* 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
* [ruby-core:110278] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods
2022-10-10 19:30 [ruby-core:110250] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods jonathanhefner (Jonathan Hefner)
` (7 preceding siblings ...)
2022-10-12 5:08 ` [ruby-core:110268] " byroot (Jean Boussier)
@ 2022-10-13 16:36 ` jonathanhefner (Jonathan Hefner)
2022-10-17 1:12 ` [ruby-core:110326] " hsbt (Hiroshi SHIBATA)
` (3 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: jonathanhefner (Jonathan Hefner) @ 2022-10-13 16:36 UTC (permalink / raw)
To: ruby-core
Issue #19047 has been updated by jonathanhefner (Jonathan Hefner).
> Thank you, now the issue is that `delegate` doesn't have a maintainer: https://github.com/ruby/ruby/commit/1159dbf305603b60a1e5d2b9ff77a9cf30775296
What is involved in being a maintainer? Is that something I can volunteer for?
----------------------------------------
Bug #19047: DelegateClass displays "method redefined" warning when overriding methods
https://bugs.ruby-lang.org/issues/19047#change-99565
* 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
* [ruby-core:110326] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods
2022-10-10 19:30 [ruby-core:110250] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods jonathanhefner (Jonathan Hefner)
` (8 preceding siblings ...)
2022-10-13 16:36 ` [ruby-core:110278] " jonathanhefner (Jonathan Hefner)
@ 2022-10-17 1:12 ` hsbt (Hiroshi SHIBATA)
2022-12-01 5:15 ` [ruby-core:111105] " matz (Yukihiro Matsumoto)
` (2 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: hsbt (Hiroshi SHIBATA) @ 2022-10-17 1:12 UTC (permalink / raw)
To: ruby-core
Issue #19047 has been updated by hsbt (Hiroshi SHIBATA).
Status changed from Open to Closed
Merged at https://bugs.ruby-lang.org/projects/ruby-master/repository/git/revisions/60610031009e60bdfe5775b0316df251ee36a973
----------------------------------------
Bug #19047: DelegateClass displays "method redefined" warning when overriding methods
https://bugs.ruby-lang.org/issues/19047#change-99618
* 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/
^ permalink raw reply [flat|nested] 14+ messages in thread
* [ruby-core:111105] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods
2022-10-10 19:30 [ruby-core:110250] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods jonathanhefner (Jonathan Hefner)
` (9 preceding siblings ...)
2022-10-17 1:12 ` [ruby-core:110326] " hsbt (Hiroshi SHIBATA)
@ 2022-12-01 5:15 ` 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)
12 siblings, 0 replies; 14+ messages in thread
From: matz (Yukihiro Matsumoto) @ 2022-12-01 5:15 UTC (permalink / raw)
To: ruby-core
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/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/
^ permalink raw reply [flat|nested] 14+ messages in thread
* [ruby-core:111123] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods
2022-10-10 19:30 [ruby-core:110250] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods jonathanhefner (Jonathan Hefner)
` (10 preceding siblings ...)
2022-12-01 5:15 ` [ruby-core:111105] " matz (Yukihiro Matsumoto)
@ 2022-12-01 8:52 ` byroot (Jean Boussier)
2022-12-01 12:40 ` [ruby-core:111127] " matz (Yukihiro Matsumoto)
12 siblings, 0 replies; 14+ messages in thread
From: byroot (Jean Boussier) @ 2022-12-01 8:52 UTC (permalink / raw)
To: ruby-core
Issue #19047 has been updated by byroot (Jean Boussier).
> we missed the warnings from DelegateClass misuse
Do you have a reference to that?
----------------------------------------
Bug #19047: DelegateClass displays "method redefined" warning when overriding methods
https://bugs.ruby-lang.org/issues/19047#change-100397
* 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/
^ permalink raw reply [flat|nested] 14+ messages in thread
* [ruby-core:111127] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods
2022-10-10 19:30 [ruby-core:110250] [Ruby master Bug#19047] DelegateClass displays "method redefined" warning when overriding methods jonathanhefner (Jonathan Hefner)
` (11 preceding siblings ...)
2022-12-01 8:52 ` [ruby-core:111123] " byroot (Jean Boussier)
@ 2022-12-01 12:40 ` matz (Yukihiro Matsumoto)
12 siblings, 0 replies; 14+ messages in thread
From: matz (Yukihiro Matsumoto) @ 2022-12-01 12:40 UTC (permalink / raw)
To: ruby-core
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/
^ 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).