Extended information about a method's caller

Is there any way to get a list of the arguments passed to a method’s caller? I am aware of Kernel.caller, but it seems like it just gives a method name. I would like something like this:

class Test
def test1(x, y)
test2
end
end

class Test2
def test
# here I’d like to have an Array containing two 2’s (the
# values passed to Test.test1)
end
end

Test.new.test1(2, 2)

···

No banners. No pop-ups. No kidding.
Introducing My Way - http://www.myway.com

“Bill Atkins” batkins57@myway.com schrieb im Newsbeitrag
news:20040312040924.AB9DF3977@mprdmxin.myway.com

Is there any way to get a list of the arguments passed to a method’s
caller? I am aware of Kernel.caller, but it seems like it just gives a
method name. I would like something like this:

class Test
def test1(x, y)
test2
end
end

class Test2
def test
# here I’d like to have an Array containing two 2’s (the
# values passed to Test.test1)
end
end

Test.new.test1(2, 2)

If you know the argument names you might be able to hack something together
with set_trace_func. But why don’t you just pass them on? :slight_smile:

Regards

robert

Or, if you hacked something together with set_trace_func, you might[1]
be able to know the argument names. :slight_smile:

module Kernel
set_trace_func proc {|*a|
$bind = a[4] unless a[3] == :argument_values
}

def argument_values
  eval <<-'CODE', $bind
    caller(0)[0] =~ /`(.*)'/ or return
    $arity = method($1).arity
    $args = local_variables[0, $arity.abs]
    $args[-1] = '*' + $args[-1] if $arity < 0
    eval "[#{$args * ','}]"
  CODE
end

end

class Test
def test(x, *y)
i = 0
z = 0…9
p argument_values
end
end

Test.new.test(4, 5, 6)

[1] The ordering of local variables returned by local_variables
may be accidental.

···

“Robert Klemme” bob.news@gmx.net wrote:

“Bill Atkins” batkins57@myway.com schrieb im Newsbeitrag
news:20040312040924.AB9DF3977@mprdmxin.myway.com

Is there any way to get a list of the arguments passed to a method’s
caller? I am aware of Kernel.caller, but it seems like it just gives a
method name.

If you know the argument names you might be able to hack something together
with set_trace_func. But why don’t you just pass them on? :slight_smile: