ruby / forwardable

Provides delegation of specified methods to a designated object
BSD 2-Clause "Simplified" License
41 stars 17 forks source link

Make def_*_delegator return name of method defined (Fixes #10) #11

Closed jeremyevans closed 4 years ago

jeremyevans commented 4 years ago

This restores compatibility with previous versions. This behavior was previously undefined, but it makes sense for the name of the defined method to be returned.

ashmaroli commented 4 years ago

Upon further thought to this, I think it'd be better to use a guard clause instead of initializing a local variable.

  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

    unless RUBY_VERSION >= '2.7'
      return mod.module_eval(&gen)
    end

    mod.send(:ruby2_keywords, ali)
  end
jeremyevans commented 4 years ago

@ashmaroli I don't think that will work on Ruby 2.7+.

ashmaroli commented 4 years ago

I see.. Good to know that. Thanks.