-
Notifications
You must be signed in to change notification settings - Fork 17
Closed
Description
In v1.2.0, def_delegator :@obj, :data, :fallback_data would return :fallback_data but v1.3.0 just returns nil
The cause for this is the last expression mod.send(:ruby2_keywords, ali) if RUBY_VERSION >= '2.7' in the following definition:
forwardable/lib/forwardable.rb
Lines 182 to 189 in e56f0f8
| def def_instance_delegator(accessor, method, ali = method) | |
| gen = Forwardable._delegator_method(self, accessor, method, ali) | |
| # If it's not a class or module, it's an instance | |
| mod = Module === self ? self : singleton_class | |
| mod.module_eval(&gen) | |
| mod.send(:ruby2_keywords, ali) if RUBY_VERSION >= '2.7' | |
| end |
The last expression is evaluated only if RUBY_VERSION >= '2.7'.
The preferred solution would be to stash the result of the previous expression and return that stashed value for Ruby 2.6 and older:
def def_instance_delegator(accessor, method, ali = method)
gen = Forwardable._delegator_method(self, accessor, method, ali)
# If it's not a class or module, it's an instance
mod = Module === self ? self : singleton_class
# stash in a lvar
result = mod.module_eval(&gen)
RUBY_VERSION >= '2.7' ? mod.send(:ruby2_keywords, ali) : result
endBased on the diff of #5, this issue would affect :single_delegator as well.
/cc @jeremyevans @hsbt Requesting a patch to be shipped at the earliest.
Thank you.
Metadata
Metadata
Assignees
Labels
No labels