caller
caller(p1 = v1)Returns the current execution stack—an array containing strings in the form “file:line” or “file:line: in `method”‘. The optional start parameter determines the number of initial stack entries to omit from the result.
Returns nil if start is greater than the size of current execution stack.
def a(skip) caller(skip) end def b(skip) a(skip) end def c(skip) b(skip) end c(0) #=> ["prog:2:in `a'", "prog:5:in `b'", "prog:8:in `c'", "prog:10:in `<main>'"] c(1) #=> ["prog:5:in `b'", "prog:8:in `c'", "prog:11:in `<main>'"] c(2) #=> ["prog:8:in `c'", "prog:12:in `<main>'"] c(3) #=> ["prog:13:in `<main>'"] c(4) #=> [] c(5) #=> nil
1Note
Reports originally defined method names, not invoked names in Ruby 1.9.x
In Ruby 1.8.7, the reported method names were those of the methods actually invoked, so if #b was an alias for #a, and #b was called, it would be reported as "... in b'". In Ruby 1.9, the same invocation is now reported as "... in a'".
Unfortunately, this change disables the hack that could formerly be used to create a variant of method that returns the method as actually invoked. The new callee method is no help with that, because it is currently synonymous with method.