Skip to content

def_*_delegator from v1.3.0 does not return name of delegator method in Ruby 2.6 and older #10

@ashmaroli

Description

@ashmaroli

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:

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
 end

Based 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions