MRI 2.2.2 / JRuby 9.0.4.0 difference given explicit `self` receiver for private method call. #3573

Closed
arronmabrey opened this Issue Dec 30, 2015 · 3 comments

Comments

Projects
None yet
2 participants
@arronmabrey
Given the following example:
class Foo
  def a
    self.b += 1
  end

  private

  def b=(v)
    @b = v
  end

  def b
    @b || 10
  end
end

Foo.new.a == 11
Under MRI ruby 2.2.2p95:
  • This works as expected and outputs true
Under JRuby 9.0.4.0 (2.2.2):
  • This raises an exception NoMethodError: private methodb' called for #Foo:0x543788f3`

I'm not sure if this is a know issue or not, I'm very new to JRuby and this was a surprise I stumbled across.

@enebo enebo added the ir label Dec 31, 2015

@enebo enebo added this to the JRuby 9.0.5.0 milestone Dec 31, 2015

@enebo

This comment has been minimized.

Show comment
Hide comment
@enebo

enebo Dec 31, 2015

Member

Just an oversight. We try and be compatible as we can but some times things get missed. In this case, MRI is being inconsistent but I sort of remember this change (and I am surprised we did not impl it). Consider a version of a:

def a
  self.b
end

Boom private method. For the sake of += it reasons you are using self so it has access to call both b and b=. I am ok with this but then why does my self.b fail? In any case, we will fix it...

Member

enebo commented Dec 31, 2015

Just an oversight. We try and be compatible as we can but some times things get missed. In this case, MRI is being inconsistent but I sort of remember this change (and I am surprised we did not impl it). Consider a version of a:

def a
  self.b
end

Boom private method. For the sake of += it reasons you are using self so it has access to call both b and b=. I am ok with this but then why does my self.b fail? In any case, we will fix it...

@enebo

This comment has been minimized.

Show comment
Hide comment
@enebo

enebo Dec 31, 2015

Member

Another thing which bugs me about this behavior and I am only documenting this for fun...It is great this is static analysis feature of the language because:

def a
  o = self
  o.b += 1
end

Will not work. It still bugs me because we are calling be against self. If static form of self is ok then why not dynamic one. Perhaps the obvious answer is it would penalize all callsites since you would need to check against self receiver per call...

Member

enebo commented Dec 31, 2015

Another thing which bugs me about this behavior and I am only documenting this for fun...It is great this is static analysis feature of the language because:

def a
  o = self
  o.b += 1
end

Will not work. It still bugs me because we are calling be against self. If static form of self is ok then why not dynamic one. Perhaps the obvious answer is it would penalize all callsites since you would need to check against self receiver per call...

@enebo

This comment has been minimized.

Show comment
Hide comment
@enebo

enebo Dec 31, 2015

Member

test_assign_private_self in MRI tests test part of the cases I fixed but I had to open #3575 because there is an unrelated set of failures in that same test.

Member

enebo commented Dec 31, 2015

test_assign_private_self in MRI tests test part of the cases I fixed but I had to open #3575 because there is an unrelated set of failures in that same test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment