__callee__ 和 __method__ 之间的区别 [英] Difference between __callee__ and __method__
本文介绍了__callee__ 和 __method__ 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Ruby 中,你可以使用其中之一
In Ruby, one can use either
__callee__
或
__method__
查找当前正在执行的方法的名称.
to find the name of the currently executing method.
两者有什么区别?
推荐答案
__method__
静态查找名称,它指的是最近的词法封闭方法定义的名称.__callee__
动态查找名称,它指的是调用该方法的名称.两者都不需要对应最初发送的消息:
__method__
looks up the name statically, it refers to the name of the nearest lexically enclosing method definition. __callee__
looks up the name dynamically, it refers to the name under which the method was called. Neither of the two necessarily needs to correspond to the message that was originally sent:
class << (foo = Object.new)
def bar(*) return __method__, __callee__ end
alias_method :baz, :bar
alias_method :method_missing, :baz
end
foo.bar # => [:bar, :bar]
foo.baz # => [:bar, :baz]
foo.qux # => [:bar, :method_missing]
这篇关于__callee__ 和 __method__ 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文