Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proc#ruby2_keywords should not capture self when used as a method #2209

Open
seven1m opened this issue Jul 12, 2024 · 0 comments
Open

Proc#ruby2_keywords should not capture self when used as a method #2209

seven1m opened this issue Jul 12, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@seven1m
Copy link
Member

seven1m commented Jul 12, 2024

Defining a method using a proc that had ruby2_keywords called on it should not capture self. I only found this because I'm working on adding delegate.rb which does this:

def Delegator.delegating_block(mid) # :nodoc:
  lambda do |*args, &block|
    target = self.__getobj__
    target.__send__(mid, *args, &block)
  end.ruby2_keywords
end

# ...

define_method(method, Delegator.delegating_block(method))

In that code, with our implementation, the self object is wrong.

Here is a test case if we ever get around to fixing this:

describe '#ruby2_keywords' do
  it 'does not capture self when used as a method' do
    x = 9
    klass = Class.new do
      define_method(:plain, ->(*) { [self, x] })
      define_method(:wrapped, ->(*) { [self, x] }.ruby2_keywords)
    end
    obj = klass.new
    obj.wrapped.first.should == obj
    obj.wrapped.last.should == 9
    obj.plain.should == obj.wrapped
  end
end

...but I have to admit, it feels like a waste to spend too much time on it since this is a remnant of by-gone era in Ruby. Maybe we'll get lucky and this method will get removed in a future Ruby version.

@seven1m seven1m added the bug Something isn't working label Jul 12, 2024
seven1m added a commit that referenced this issue Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant