public Method

Kernel.caller(...)

caller(start=1)     array

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.

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"]
c(1)   #=> ["prog:5:in `b'", "prog:8:in `c'", "prog:11"]
c(2)   #=> ["prog:8:in `c'", "prog:12"]
c(3)   #=> ["prog:13"]

Source Code

/*
*  call-seq:
*     caller(start=1)    => array
*  
*  Returns the current execution stack---an array containing strings in
*  the form ``<em>file:line</em>'' or ``<em>file:line: in
*  `method'</em>''. The optional _start_ parameter
*  determines the number of initial stack entries to omit from the
*  result.
*     
*     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"]
*     c(1)   #=> ["prog:5:in `b'", "prog:8:in `c'", "prog:11"]
*     c(2)   #=> ["prog:8:in `c'", "prog:12"]
*     c(3)   #=> ["prog:13"]
*/

static VALUE
rb_f_caller(argc, argv)
   int argc;
   VALUE *argv;
{
   VALUE level;
   int lev;

   rb_scan_args(argc, argv, "01", &level);

   if (NIL_P(level)) lev = 1;
   else lev = NUM2INT(level);
   if (lev < 0) rb_raise(rb_eArgError, "negative level (%d)", lev);

   return backtrace(lev);
}
Comments

Have your say
Please use Textile formatting (click here for a cheat sheet). Use <code/> and <pre/> for code samples.
Click here to login with OpenID to to post comments.