Open jonathanhefner opened 1 year ago
Why not prepend Helper
?
@nobu sure you can do that, but you'd have a similar-ish issue with just defining a method in the delegator:
Foo = Struct.new(:field)
FooDelegator = DelegateClass(Foo) do
def field
super.to_s
end
end
In this example super
doesn't call the method that DelegateClass
generated, but fallback to Delegator#method_missing
, it works but kinda defeat the purpose.
It's somewhat assumed that if you create a delegator, you will want to specialize a few methods.
Before this commit, modules included in a
DelegateClass
could not override delegate methods:This commit defines delegate methods in a separate module, so other modules can come before it in the method lookup chain:
Also, because of this change, methods in a
DelegateClass
block will properly override instead of redefine. Therefore, callingsuper
is faster:Benchmark script
Before
After
Fixes https://bugs.ruby-lang.org/issues/19079.