Is the JRuby implementation of the Kernel #___ method broken?

This is the description of Kernel#__method__ according to Ruby-Doc.org (highlighted by me):

Returns the name when defining the current method as a character. If called outside the method, it returns nil .

Now consider the following code snippet:

 DEFINITION = proc { __method__ } class C define_method :one, DEFINITION define_method :two, DEFINITION end o = C.new 

When I run the following using MRI v1.8.7 +, I get the expected results:

 o.one #=> :one o.two #=> :two 

However, when I run the same code using JRuby 1.7+ (I have not tested previous versions):

 o.one #=> :two o.two #=> :two 

Can this be considered a defect in the JRuby implementation, or is it just a different interpretation of Kernel#__method__ ?

+1
source share
1 answer

It may be a defect in the implementation of JRuby __method__ , or it may be an error in the implementation of define_method , or it may be strictly limited to using the two together. See what happens if you inject a Proc object into a block using the & operator:

 DEFINITION = proc { __method__ } class C define_method :one, &DEFINITION define_method :two, &DEFINITION end o = C.new 

Now in MRI, as before:

 o.one #=> :one o.two #=> :two 

However, in JRuby, he fixed:

 o.one #=> :one o.two #=> :two 

Given the internal implementation of MRI define_method , which includes processing Proc arguments against block arguments, if JRuby is not at all similar, it is also possible that this is the problem.

In any case, there are no parallels that can be found by replacing __method__ with self , binding , object_id or any combination or permutation of them, so the problem is certainly localized to use __method__ .

UPDATE: completion of the twist

This was a known bug in MRI 1.9.2 , and the JRuby implementation reflects this behavior.

+4
source

Source: https://habr.com/ru/post/1341472/


All Articles