Menu

Getting the Caller Method in Ruby

Need to know which method called you ? Its as simple as:caller = CallChain.caller_method

Alternatively it could be added to Object, but that would be dirty 😉

class CallChain
def self.caller_method(depth=1)
parse_caller(caller(depth+1).first).last
end
private
#Stolen from ActionMailer, where this was used but was not made reusable
def self.parse_caller(at)
if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at
file = Regexp.last_match[1]
line = Regexp.last_match[2].to_i
method = Regexp.last_match[3]
[file, line, method]
end
end
end