# Inspetting a method's caller

**URL:** <https://www.ruby-forum.com/t/inspetting-a-methods-caller/132367>\
**Category:** Ruby\
**Created:** [March 19, 2008, 1:53pm UTC](https://www.ruby-forum.com/t/inspetting-a-methods-caller/132367 "2008-03-19T13:53:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![albertosesena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@albertosesena](https://www.ruby-forum.com/u/albertosesena)\
**Post date:** [March 19, 2008, 1:53pm UTC](https://www.ruby-forum.com/t/inspetting-a-methods-caller/132367/1 "2008-03-19T13:53:43Z")

</div>

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

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

class B  
def do\_xyz(n)  
# -\> Here i want to know that  
# -\> This method has been called  
# -\> From A#do\_stg  
n + 1  
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 🙂

---

<div class="post-metadata">

**Author:** ![albertosesena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@albertosesena](https://www.ruby-forum.com/u/albertosesena)\
**Post date:** [March 19, 2008, 2:32pm UTC](https://www.ruby-forum.com/t/inspetting-a-methods-caller/132367/2 "2008-03-19T14:32:38Z")

</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 S.

---

<div class="post-metadata">

**Author:** ![albertosesena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@albertosesena](https://www.ruby-forum.com/u/albertosesena)\
**Post date:** [March 19, 2008, 3:36pm UTC](https://www.ruby-forum.com/t/inspetting-a-methods-caller/132367/3 "2008-03-19T15:36:42Z")

</div>

On Wed, Mar 19, 2008 at 9:10 AM, Alberto S.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> but i guess there are hundreds. Obviously, i can implement DBHelper#find  
> It is just _not what i want to do_.

There’s something in Ruby F.s 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

---

<div class="post-metadata">

**Author:** ![albertosesena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@albertosesena](https://www.ruby-forum.com/u/albertosesena)\
**Post date:** [March 19, 2008, 3:10pm UTC](https://www.ruby-forum.com/t/inspetting-a-methods-caller/132367/4 "2008-03-19T15:10:43Z")

</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 = {})  
…  
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.

---

<div class="post-metadata">

**Author:** ![albertosesena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@albertosesena](https://www.ruby-forum.com/u/albertosesena)\
**Post date:** [March 19, 2008, 5:35pm UTC](https://www.ruby-forum.com/t/inspetting-a-methods-caller/132367/5 "2008-03-19T17:35:57Z")

</div>

Alberto S. 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:** ![albertosesena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f9ae1b/32.png) [@albertosesena](https://www.ruby-forum.com/u/albertosesena)\
**Post date:** [March 19, 2008, 4:04pm UTC](https://www.ruby-forum.com/t/inspetting-a-methods-caller/132367/6 "2008-03-19T16:04:27Z")

</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  
def foo  
puts self.class  
end  
end

class A  
include M  
end

class B  
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.
