Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

method visibility is always private when defined in class_eval #2248

Closed
ConradIrwin opened this Issue · 2 comments

2 participants

@ConradIrwin

There are two intertwined bugs:

  1. Method visibility is not public by default inside class_eval
  2. Method visibility set by public, protected or private is ignored in an eval binding inside a block

i.e. both of the following fail

Object.class_eval do
  def foo; 5; end
end
self.foo.should == 5
#=> NoMethodError: private method `foo' called on an instance of Object.
Object.class_eval do
  public
  def foo; 5; end
end
self.foo.should == 5
#=> NoMethodError: private method `foo' called on an instance of Object.

I think 1. can be fixed by passing a flag down from Kernel#module_eval through to wherever the variable scope is created, and 2. can be fixed by adding return @method_visibility if defined?(@method_visibility) to the top of VariableScope#method_visibility (it's a bit convoluted because public seems to be represented by nil).

@dbussink
Owner

I just ran that code here locally and it works fine. I think this only happens inside irb. This is probably a duplicate of #2089

@ConradIrwin

I noticed it with pry's method redefinition features (they also use TOPLEVEL_BINDING.eval) so it sounds like a duplicate, yes.

@ghost Unknown referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
@strcmp strcmp referenced this issue from a commit
Commit has since been removed from the repository and is no longer available.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.