Project

General

Profile

Actions

Bug #19047

closed

DelegateClass displays "method redefined" warning when overriding methods

Added by jonathanhefner (Jonathan Hefner) about 2 years ago. Updated almost 2 years ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.1.2p20
[ruby-core:110250]

Description

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:

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.

I would be happy to submit a PR to https://github.com/ruby/delegate if this is something we want to address.


Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #19079: Modules included in a DelegateClass cannot override delegate methodsRejectedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0