From: Mike Stok Date: 2003-01-16T04:19:20+09:00 Subject: Re: Doc Strings? In article <3E25ADE1.5050700@chadfowler.com>, Chad Fowler wrote: > [...] This brings me to a question: How do > you find out where a method is called from? I used to do this in Java > through some nasty stack trace hacks (throw an exception and > capture/parse the stack trace string). I seem to remember when I was > first learning Ruby a couple of years ago that there is a better way to > do this in Ruby, but I can't seem to specifically remember what it was. > > Anyone know? Do you mean caller? [mike@ratdog mike]$ ri caller --------------------------------------------------------- Kernel::caller caller( [anInteger] ) -> anArray ------------------------------------------------------------------------ Returns the current execution stack---an array containing strings in the form ``file:line'' or ``file:line: in `method'''. The optional anInteger 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"] Hope this helps, Mike -- mike@stok.co.uk | The "`Stok' disclaimers" apply. http://www.stok.co.uk/~mike/ | GPG PGP Key 1024D/059913DA mike@exegenix.com | Fingerprint 0570 71CD 6790 7C28 3D60 http://www.exegenix.com/ | 75D2 9EC4 C1C0 0599 13DA