Project

General

Profile

Actions

Bug #19079

closed

Modules included in a DelegateClass cannot override delegate methods

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

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 3.1.2p20
[ruby-core:110483]

Description

Because DelegateClass defines delegate methods on the class itself, those delegate methods come first in the method lookup chain. This prevents included modules from overriding delegate methods:

Base = Class.new do
  def foo
    "base"
  end
end

Helper = Module.new do
  def foo
    "helper"
  end
end

WithHelper = DelegateClass(Base) { include Helper }

WithHelper.new(Base.new).foo
# => "base"

One possible solution would be to define the delegate methods in a separate module. That way, other modules could come before it in the method lookup chain.


Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #19047: DelegateClass displays "method redefined" warning when overriding methodsClosedActions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0