In Ruby, one can use either
__callee__
or
__method__
to find the name of the currently executing method.
What is the difference between the two?
In Ruby, one can use either
__callee__
or
__method__
to find the name of the currently executing method.
What is the difference between the two?
To paraphrase the documentation,
__callee__
is the name of the method that the caller called, whereas__method__
is the name of the method at definition. The following example illustrates the difference:If I call
Foo.new.foo
then the output isbut if I call
Foo.new.bar
then the output is__method__
returns:foo
in both cases because that is the name of the method as defined (i.e. the class hasdef foo
), but in the second example the name of the method the caller is calling isbar
and so__callee__
returns that.__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:__method__ returns defined name, and __callee__ returns called name. They are same usually, but different in a aliased method.
link