How can I get the callers __LINE__ and __FILE__?

Have a look at Kernel#caller

dave[Downloads/emacs 13:46:32] ri caller
---------------------------------------------------------- Kernel#caller
caller(start=1) => array

···

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


  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”]

Cheers

Dave

Thanks, thats what I want.

Looking around, I can’t seem to find a way to introspect the stack,
other than that. I.e., to look at my callers context, see what it’s
local variables are, etc.

This kind of feature would be interesting, I could do

def pp_local
ctx = stack[1] # assume current stack is stack[0]
puts “in method #{ctx.method_name},”
ctx.locals.each { |sym, o|
print "#{sym} has value "
pp o
end
end

Is anything like this currently possible?

Thanks,
Sam

Quoteing dave@pragprog.com, on Sun, Mar 07, 2004 at 04:47:25AM +0900:

···

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”]

Cheers

Dave

Sam Roberts wrote:

Thanks, thats what I want.

Looking around, I can’t seem to find a way to introspect the stack,
other than that. I.e., to look at my callers context, see what it’s
local variables are, etc.

This kind of feature would be interesting, I could do

def pp_local
ctx = stack[1] # assume current stack is stack[0]
puts “in method #{ctx.method_name},”
ctx.locals.each { |sym, o|
print "#{sym} has value "
pp o
end
end

Is anything like this currently possible?

Thanks,
Sam

It’s not quite what you want but you access your callers context by
setting a trace function to the binding for the calling scope.

Something like this

class ContextStack
attr_accessor :stack

INIT_STACK_TIMES = 3
CALL_STACK_OFFSET = 3

def initialize
@stack = [TOPLEVEL_BINDING] * INIT_STACK_TIMES
end

def trace_func(event, file, line, id, binding)
case event
when ‘call’, ‘class’
@stack.push binding
when ‘return’, ‘end’
@stack.pop
end
end

@@context_stack = ContextStack.new
set_trace_func proc {|event, file, line, id, binding, klass|
@@context_stack.trace_func(event, file, line, id, binding)
}

def ContextStack.get_var(level, variable)
eval “#{variable}”, @@context_stack.stack[level]
end

def ContextStack.set_var(level, variable, value)
eval “#{variable}=#{value}”, @@context_stack.stack[level]
end
end

def test()
puts “x = #{ContextStack.get_var(1, “x”)}”

ContextStack.set_var(1, “x”, 10)
end

x = 5

test()

puts “now x = #{x}”

HTH

···


Mark Sparshatt

“Dave Thomas” dave@pragprog.com schrieb im Newsbeitrag
news:12E1B815-6FA7-11D8-B7E7-000A95676A62@pragprog.com

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
···

On Mar 6, 2004, at 13:29, Sam Roberts wrote: