How to figure out the sender?

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

robert

“T.Oakley” root@127.0.0.1 schrieb im Newsbeitrag
news:opr55abuaexses1n@news.elisa.net

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 :slight_smile:

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

I have the same question. I need to know who called a method, so please
let me know too!

thx

···

On 0409, Robert Klemme wrote:

±[ Kontra, Gergelykgergely@mcl.hu PhD student Room IB113 ]---------+

http://www.mcl.hu/~kgergely “Olyan langesz vagyok, hogy |
Mobil:(+36 20) 356 9656 ICQ: 175564914 poroltoval kellene jarnom” |
±- Magyar php mirror es magyar php dokumentacio: http://hu.php.net --+

Maybe it is not so nice to discriminate on the caller :slight_smile:
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

···

On Fri, 09 Apr 2004 12:21:39 +0200, Gergely Kontra wrote:

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

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

def callme
$tracer.invoker
end
callme
=> main