JRuby的Kernel #__ method__的实现是否被破坏? [英] Is JRuby's implementation of Kernel#__method__ broken?
问题描述
这是Kernel#__method__
的描述根据Ruby-Doc.org (强调):
在当前方法的定义处返回名称 作为符号.如果在方法外部调用,它将返回
nil
.
Returns the name at the definition of the current method as a Symbol. If called outside of a 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
当我使用MRI v1.8.7 +运行以下命令时,我得到了预期的结果:
When I run the following using MRI v1.8.7+ I'm getting the expected results:
o.one #=> :one
o.two #=> :two
但是,当我使用JRuby 1.7+运行相同的代码时(我尚未测试以前的版本):
However when I run the same code using JRuby 1.7+ (I haven't tested the previous versions):
o.one #=> :two
o.two #=> :two
这是否可以视为JRuby实现中的缺陷,或者仅仅是对Kernel#__method__
的另一种解释?
Could this be considered a defect in JRuby's implementation or is it simply a different interpretation of Kernel#__method__
?
推荐答案
这可能是JRuby实现的__method__
的缺陷,或者可能是实现的define_method
的错误,或者可能受到严格限制两者一起使用.看看使用&
运算符将Proc
对象转换为块时会发生什么:
It may be a defect in JRuby's implementation of __method__
, or it may be a bug in the implementation of define_method
, or it may be strictly limited to the use of the two together. Look what happens if you cast the Proc
object into a block by using the &
operator:
DEFINITION = proc { __method__ }
class C
define_method :one, &DEFINITION
define_method :two, &DEFINITION
end
o = C.new
现在像以前一样在MRI中:
Now in MRI, as before:
o.one #=> :one
o.two #=> :two
但是,在JRuby中,它是固定的:
However, in JRuby, it's fixed:
o.one #=> :one
o.two #=> :two
考虑到MRI的define_method
的内部实现,其中包括Proc
自变量与块自变量的处理,如果JRuby的相似之处,也可能是问题所在.
Given the internal implementation of MRI's define_method
, which includes the handling of Proc
arguments vs block arguments, if JRuby's is at all similar, it's also possible that that is where the problem could lie.
无论哪种方式,都无法通过将__method__
替换为self
,binding
,object_id
或它们的任何组合或排列来找到相似之处,因此,该问题肯定是局限在
Either way, there are no parallels to be found by replacing __method__
with self
, binding
, object_id
, or any combination or permutation of them, so the issue is certainly localized to uses of __method__
.
更新:扭曲结尾
这是MRI中的一个已知错误1.9.2 ,并且JRuby的实现反映了该行为.
This was a known bug in MRI 1.9.2, and JRuby's implementation mirrors that behavior.
这篇关于JRuby的Kernel #__ method__的实现是否被破坏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!