How to get parameter names when in set_trace_func callback?

S

Stephen Kellett

Hi folks,

When my code is executing my trace_function set using set_trace_func how
do I get the parameter names of the function that is being traced? Say
for example I had a function

def myFunc(firstName, secondName, age)
do whatever 1
do whatever 2
do whatever 3
end

and my trace function has been called for a line execution event for "do
whatever 2". I can get the filename and line number from the binding.
How do I get the parameter names?

I've spent all this afternoon trying to do this, examining the Ruby
source, but can't work it out. Hope someone can help me, or tell me it
can't be done :-(

Stephen
 
K

Kent Sibilev

AFAIK, There is no official way to get names of method parameters. But
you can try this:

def m(a,b)
end

tracer = lambda do |*args|
p eval("local_variables", args[4]) if args[0] == 'call'
end

set_trace_func tracer
m(1, 2)

Cheers,
Kent.
 
S

Stephen Kellett

Kent said:
AFAIK, There is no official way to get names of method parameters. But
you can try this:

def m(a,b)
end

tracer = lambda do |*args|
p eval("local_variables", args[4]) if args[0] == 'call'
end

set_trace_func tracer
m(1, 2)

I think what you are saying is that when it is "call", local variables
actually has a copy of the parameters, even though at a subsequent
"line" - say the next statement executed - the local variables may hold
more data items. Hadn't thought about it that way.

Thanks

Stephen
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top