How to get the name of the calling method?
is there a way in Ruby to find the calling method name inside of a method?
For example:
class Test
def self.foo
Fooz.bar
end
end
class Fooz
def self.bar
# get Test.foo or foo
end
end
puts caller[0]
or perhaps...
puts caller[0][/`.*'/][1..-2]
In Ruby 2.0.0, you can use:
caller_locations(1,1)[0].label
It's much faster than the Ruby 1.8+ solution:
caller[0][/`([^']*)'/, 1]
Will get included in backports
when I get the time (or a pull request!).
Use caller_locations(1,1)[0].label
(for ruby >= 2.0)
Edit: My answer was saying to use __method__
but I was wrong, it returns the current method name.