# Inspetting a method's caller

**URL:** <https://rubytalk.org/t/inspetting-a-methods-caller/45279>\
**Category:** ruby-talk\
**Created:** [19 March 2008 12:53 UTC](https://rubytalk.org/t/inspetting-a-methods-caller/45279 "2008-03-19T12:53:39Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alberto\_Santini](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@Alberto\_Santini](https://rubytalk.org/u/Alberto_Santini)\
**Post date:** [19 March 2008 12:53 UTC](https://rubytalk.org/t/inspetting-a-methods-caller/45279/1 "2008-03-19T12:53:39Z")

</div>

Hello.  
How can i know a function's caller method? E.g.

class A  
&nbsp;&nbsp;def do\_stg(n)  
&nbsp;&nbsp;&nbsp;&nbsp;b = B.new  
&nbsp;&nbsp;&nbsp;&nbsp;b.do\_xyz(n)  
&nbsp;&nbsp;end  
end

class B  
&nbsp;&nbsp;def do\_xyz(n)  
&nbsp;&nbsp;&nbsp;&nbsp;# -\> Here i want to know that  
&nbsp;&nbsp;&nbsp;&nbsp;# -\> This method has been called  
&nbsp;&nbsp;&nbsp;&nbsp;# -\> From A#do\_stg  
&nbsp;&nbsp;&nbsp;&nbsp;n + 1  
&nbsp;&nbsp;end  
end

Kernel#caller does not help.. i don't care what file or line a function  
has been called, and i couldn't find nothing helpful on ruby-doc nor by  
searching google 😑  
Please keep in mind i'd like to know that do\_xyz has been called by  
do\_stg -\> WHICH IS A METHOD OF THE "A" CLASS \<-

Thanks for your help 🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![mt1](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@mt1](https://rubytalk.org/u/mt1)\
**Post date:** [19 March 2008 13:31 UTC](https://rubytalk.org/t/inspetting-a-methods-caller/45279/2 "2008-03-19T13:31:41Z")

</div>

I've always seen that whenever you need something like this, there's  
always a better design choice that you can make. Like what would you  
do in languages that didn't have caller etc etc.

Its still a hack, (because ideally you shouldn't be needing this IMO),  
but you can make the caller send in an extra argument, putting its  
description/ID/Name there.

MT

> **···**
>
> On Wed, Mar 19, 2008 at 8:53 PM, Alberto Santini \<santini.alberto@gmail.com\> wrote:
> 
> > Hello.  
> > How can i know a function's caller method? E.g.
> > 
> > class A  
> > &nbsp;&nbsp;def do\_stg(n)  
> > &nbsp;&nbsp;&nbsp;&nbsp;b = B.new  
> > &nbsp;&nbsp;&nbsp;&nbsp;b.do\_xyz(n)  
> > &nbsp;&nbsp;end  
> > end
> > 
> > class B  
> > &nbsp;&nbsp;def do\_xyz(n)  
> > &nbsp;&nbsp;&nbsp;&nbsp;# -\> Here i want to know that  
> > &nbsp;&nbsp;&nbsp;&nbsp;# -\> This method has been called  
> > &nbsp;&nbsp;&nbsp;&nbsp;# -\> From A#do\_stg  
> > &nbsp;&nbsp;&nbsp;&nbsp;n + 1  
> > &nbsp;&nbsp;end  
> > end
> > 
> > Kernel#caller does not help.. i don't care what file or line a function  
> > has been called, and i couldn't find nothing helpful on ruby-doc nor by  
> > searching google 😑  
> > Please keep in mind i'd like to know that do\_xyz has been called by  
> > do\_stg -\> WHICH IS A METHOD OF THE "A" CLASS \<-
> > 
> > Thanks for your help 🙂  
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Alberto\_Santini](https://avatars.discourse-cdn.com/v4/letter/a/87869e/32.png) [@Alberto\_Santini](https://rubytalk.org/u/Alberto_Santini)\
**Post date:** [19 March 2008 14:10 UTC](https://rubytalk.org/t/inspetting-a-methods-caller/45279/3 "2008-03-19T14:10:33Z")

</div>

> but you can make the caller send in an extra argument, putting its  
> description/ID/Name there.

Of course i can, i just \*don't want\* to do this. For example, imagine i  
want to access a database table that (by -\> convention \<-) has the same  
name of a class. Imagine i have a DBHelper class that contains a  
DBHelper#find method used to find records on the database. if  
DBHelper#find is called from a Ball object i want to lookup on the  
"ball" table, it it's called from a Cube object i want to search the  
"cube" table, etc. This is just the simplest usage that came to my mind,  
but i guess there are hundreds. Obviously, i can implement DBHelper#find  
in a way like this:

def find(table\_name = "", criteria = {})  
&nbsp;&nbsp;...  
end

and call it like this:

find(self.class.to\_s, {:id =\> 5})

It is just \*not what i want to do\*.

So, coming back to my question, is there a simple way to get the wanted  
class name, without haveing to parse Kernel#caller?

Thanks.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Ben\_Oakes](https://avatars.discourse-cdn.com/v4/letter/b/b5ac83/32.png) [@Ben\_Oakes](https://rubytalk.org/u/Ben_Oakes)\
**Post date:** [19 March 2008 14:35 UTC](https://rubytalk.org/t/inspetting-a-methods-caller/45279/4 "2008-03-19T14:35:06Z")

</div>

There's something in Ruby Facets that seems related: Binding#called

[http://facets.rubyforge.org/quick/rdoc/core/classes/Binding.html#M000221](http://facets.rubyforge.org/quick/rdoc/core/classes/Binding.html#M000221)

I've only ever used it when I'm working with trace statements when a  
debugger isn't available or doing the job I need it to.

-- Ben

> **···**
>
> On Wed, Mar 19, 2008 at 9:10 AM, Alberto Santini \<santini.alberto@gmail.com\> wrote:
> 
> > \> but you can make the caller send in an extra argument, putting its  
> > \> description/ID/Name there.
> > 
> > Of course i can, i just \*don't want\* to do this. For example, imagine i  
> > want to access a database table that (by -\> convention \<-) has the same  
> > name of a class. Imagine i have a DBHelper class that contains a  
> > DBHelper#find method used to find records on the database. if  
> > DBHelper#find is called from a Ball object i want to lookup on the  
> > "ball" table, it it's called from a Cube object i want to search the  
> > "cube" table, etc. This is just the simplest usage that came to my mind,  
> > but i guess there are hundreds. Obviously, i can implement DBHelper#find  
> > in a way like this:
> > 
> > def find(table\_name = "", criteria = {})  
> > &nbsp;&nbsp;...  
> > end
> > 
> > and call it like this:
> > 
> > find(self.class.to\_s, {:id =\> 5})
> > 
> > It is just \*not what i want to do\*.
> > 
> > So, coming back to my question, is there a simple way to get the wanted  
> > class name, without haveing to parse Kernel#caller?
> > 
> > Thanks.  
> > --
> > 
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Alex\_Fenton](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@Alex\_Fenton](https://rubytalk.org/u/Alex_Fenton)\
**Post date:** [19 March 2008 16:34 UTC](https://rubytalk.org/t/inspetting-a-methods-caller/45279/5 "2008-03-19T16:34:54Z")

</div>

Alberto Santini wrote:

> > but you can make the caller send in an extra argument, putting its  
> > description/ID/Name there.
> 
> Of course i can, i just \*don't want\* to do this. For example, imagine i want to access a database table that (by -\> convention \<-) has the same name of a class. Imagine i have a DBHelper class that contains a DBHelper#find method used to find records on the database. if DBHelper#find is called from a Ball object i want to lookup on the "ball" table, it it's called from a Cube object i want to search the "cube" table, etc.

Perhaps your DBHelper class would be better designed as a module that is included in the Ball/Cube.etc classes, or extends its instances?

alex

---

<div class="post-metadata">

**Author:** ![mt1](https://avatars.discourse-cdn.com/v4/letter/m/53a042/32.png) [@mt1](https://rubytalk.org/u/mt1)\
**Post date:** [19 March 2008 15:03 UTC](https://rubytalk.org/t/inspetting-a-methods-caller/45279/6 "2008-03-19T15:03:43Z")

</div>

🙂

I don't know, but maybe you can implement the methods you \*want\* as a  
module method, and use them in your classes that you need such  
functionality built-into, which would ensure they'll be able to know  
which class they're under whenever they're called.  
like

module M  
&nbsp;&nbsp;&nbsp;def foo  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts self.class  
&nbsp;&nbsp;&nbsp;end  
end

class A  
&nbsp;&nbsp;include M  
end

class B  
&nbsp;&nbsp;include M  
end

a= A.new  
a.foo =\> A  
b= B.new  
b.foo =\> B

I might be completely off base here, but i'll still \*try\* to give you  
a design based resolution, more often than not, that causes less  
problems in the long run.

HTH  
Mt.
