How to figure out the sender?

T

T.Oakley

Continuing my established tradition of stupid questions, I'm wondering how I can figure out who sent a message to an object. Couldn't figure this one out from the ref manual or the "Programming Ruby (&c)" book. I know Kernel::caller exists, but it only returns the calling method and line number in the source, nothing about which object/class the method is associated to. Being the whiny bastage I am, I'm not satisfied with this, and I want to find out all sorts of trivial information about the caller of my methods. I don't let my methods hang out with delinquent classes :)

Seriously speaking, I'm just interested in knowing information about the object that calls method xyz, like so:


class Frobble
...
...
def xyz(value)

if self.caller_class = "Moog" # #caller_class is just an example. Could be just #caller for example
return value/43
else
return value/13
end
end
...
...
end

q = Frobble.new

class Moog

...
a.baz = q.xyz(27)
...


Thanks again,

Thomas
 
R

Robert Klemme

You can use set_trace_function to record this kind of information at
runtime.

robert


T.Oakley said:
Continuing my established tradition of stupid questions, I'm wondering how
I can figure out who sent a message to an object. Couldn't figure this one
out from the ref manual or the "Programming Ruby (&c)" book. I know
Kernel::caller exists, but it only returns the calling method and line
number in the source, nothing about which object/class the method is
associated to. Being the whiny bastage I am, I'm not satisfied with this,
and I want to find out all sorts of trivial information about the caller of
my methods. I don't let my methods hang out with delinquent classes :)
Seriously speaking, I'm just interested in knowing information about the
object that calls method xyz, like so:
class Frobble
...
...
def xyz(value)

if self.caller_class = "Moog" # #caller_class is just an example.
Could be just #caller for example
 
G

Gergely Kontra

On 0409, Robert Klemme wrote:
I have the same question. I need to know who called a method, so please
let me know too!

thx
 
K

Kristof Bastiaensen

On 0409, Robert Klemme wrote:
I have the same question. I need to know who called a method, so please
let me know too!

thx

Maybe it is not so nice to discriminate on the caller :)
You could require the object to send itself, i.e.:

def mymethod(object, var)
if object.type == This
do_that
...
end
end
mymethod(self, 3)

You may figure out the sender by using the following,
but it is not recommended, because it will slow down
your program considerably:

$tracer = []

set_trace_func lambda { |e, f, l, o, b, c|
case e
when "call"
$tracer.push (eval "self", b)
when "return"
$tracer.pop
end
}

def $tracer.invoker
self[-2]
end

#-------------------------

def callme
$tracer.invoker
end
callme
=> main
 

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
474,145
Messages
2,570,824
Members
47,369
Latest member
FTMZ

Latest Threads

Top