# How to figure out the sender?

**URL:** <https://rubytalk.org/t/how-to-figure-out-the-sender/10941>\
**Category:** ruby-talk\
**Created:** [8 April 2004 17:59 UTC](https://rubytalk.org/t/how-to-figure-out-the-sender/10941 "2004-04-08T17:59:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://rubytalk.org/u/Robert)\
**Post date:** [8 April 2004 17:59 UTC](https://rubytalk.org/t/how-to-figure-out-the-sender/10941/1 "2004-04-08T17:59:16Z")

</div>

You can use set\_trace\_function to record this kind of information at  
runtime.

```
robert

```

“T.Oakley” [root@127.0.0.1](mailto:root@127.0.0.1) schrieb im Newsbeitrag  
[news:opr55abuaexses1n@news.elisa.net](mailto: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 🙂
> 
> 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

---

<div class="post-metadata">

**Author:** ![Gergely\_Kontra](https://avatars.discourse-cdn.com/v4/letter/g/7c8e57/32.png) [@Gergely\_Kontra](https://rubytalk.org/u/Gergely_Kontra)\
**Post date:** [9 April 2004 10:21 UTC](https://rubytalk.org/t/how-to-figure-out-the-sender/10941/2 "2004-04-09T10:21:39Z")

</div>

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, Gergely[kgergely@mcl.hu](mailto:kgergely@mcl.hu) PhD student Room IB113 ]---------+
> 
> > [http://www.mcl.hu/~kgergely](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](http://hu.php.net) --+

---

<div class="post-metadata">

**Author:** ![Kristof\_Bastiaensen1](https://avatars.discourse-cdn.com/v4/letter/k/9fc29f/32.png) [@Kristof\_Bastiaensen1](https://rubytalk.org/u/Kristof_Bastiaensen1)\
**Post date:** [9 April 2004 11:54 UTC](https://rubytalk.org/t/how-to-figure-out-the-sender/10941/3 "2004-04-09T11:54:16Z")

</div>

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

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