I'm trying to get a method name from itself:
def funky_method
self.inspect
end
It returns "main".
How can I return "funky_method" instead?
Here is the code:
For versions >= 1.9:
def funky_method
return __callee__
end
For versions < 1.9:
def funky_method
return __method__
end
__callee__
returns the "called name" of the current method whereas __method__
returns the "name at the definition" of the current method.
As a consequence, __method__
doesn't return the expected result when used with alias_method.
class Foo
def foo
puts "__method__: #{__method__.to_s} __callee__:#{__callee__.to_s} "
end
alias_method :baz, :foo
end
Foo.new.foo # __method__: foo __callee__:foo
Foo.new.baz # __method__: foo __callee__:baz
Very simple:
def foo
puts __method__
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With