"Dave Thomas" <dave / pragprog.com> schrieb im Newsbeitrag
news:12E1B815-6FA7-11D8-B7E7-000A95676A62 / pragprog.com...
>
> On Mar 6, 2004, at 13:29, Sam Roberts wrote:
>
> > I want a version of "pp" that prints the file/line, so I tried this:
> >
> > def debug(*objs)
> >   print __FILE__, ':', __LINE__, ':'
> >   pp(*objs)
> > end
>
> Have a look at Kernel#caller
>
> dave[Downloads/emacs 13:46:32] ri caller
> ---------------------------------------------------------- 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"]
>

def debug(*objs)
  file, line = caller[0].split(/:/)
  print file, ":", line, ":"
  pp *objs
end

def debug(*objs)
  print caller[0].gsub(/^(([^:]*:){2}).*$/, '\\1')
  pp *objs
end

....

    robert