# Self.method?

**URL:** https://rubytalk.org/t/self-method/31148
**Category:** ruby-talk
**Created:** [20 September 2006 11:05 UTC](https://rubytalk.org/t/self-method/31148 "2006-09-20T11:05:08Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![aidy2](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@aidy2](https://rubytalk.org/u/aidy2)
#### Post date: [20 September 2006 11:05 UTC](https://rubytalk.org/t/self-method/31148/1 "2006-09-20T11:05:08Z")

</div>

Hi,

We can write "self.class" to get the current object, but is it possible  
to do something like a "self.method" to retrieve the currently running  
method?

Cheers

Aidy

---

<div class="post-metadata">

### Author: ![Jordan\_Callicoat](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@Jordan\_Callicoat](https://rubytalk.org/u/Jordan_Callicoat)
#### Post date: [20 September 2006 12:20 UTC](https://rubytalk.org/t/self-method/31148/2 "2006-09-20T12:20:10Z")

</div>

aidy wrote:

> We can write "self.class" to get the current object, but is it possible  
> to do something like a "self.method" to retrieve the currently running  
> method?

Hi Aidy,

In short, no it is not possible (in an easy way) -- but just wait, and  
someone smarter than me will prove me wrong. 😉 See also Kernel#caller  
[1]

[1] [module Kernel - RDoc Documentation](http://ruby-doc.org/core/classes/Kernel.html#M001968)

Regards,  
Jordan

---

<div class="post-metadata">

### Author: ![Vincent\_Fourmond](https://avatars.discourse-cdn.com/v4/letter/v/cdc98d/32.png) [@Vincent\_Fourmond](https://rubytalk.org/u/Vincent_Fourmond)
#### Post date: [20 September 2006 13:12 UTC](https://rubytalk.org/t/self-method/31148/3 "2006-09-20T13:12:34Z")

</div>

Hello !

> In short, no it is not possible (in an easy way) -- but just wait, and  
> someone smarter than me will prove me wrong. 😉 See also Kernel#caller  
> [1]

&nbsp;&nbsp;Based on that, you could try something as follows:

def who\_am\_i?  
&nbsp;&nbsp;return caller[0]  
end

def meth  
&nbsp;&nbsp;p who\_am\_i?  
end

meth

&nbsp;&nbsp;As it returns text, you'll need some regular expression handling, but  
that could help you...

&nbsp;&nbsp;Vince, in a ruby mood today...

---

<div class="post-metadata">

### Author: ![Michael\_Guterl1](https://avatars.discourse-cdn.com/v4/letter/m/e47c2d/32.png) [@Michael\_Guterl1](https://rubytalk.org/u/Michael_Guterl1)
#### Post date: [20 September 2006 13:16 UTC](https://rubytalk.org/t/self-method/31148/4 "2006-09-20T13:16:05Z")

</div>

[http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/205118](http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/205118)

Michael Guterl

> **···**
>
> On 9/20/06, MonkeeSage \<MonkeeSage@gmail.com\> wrote:
> 
> > aidy wrote:  
> > \> We can write "self.class" to get the current object, but is it possible  
> > \> to do something like a "self.method" to retrieve the currently running  
> > \> method?
> > 
> > Hi Aidy,
> > 
> > In short, no it is not possible (in an easy way) -- but just wait, and  
> > someone smarter than me will prove me wrong. 😉 See also Kernel#caller  
> > [1]
> > 
> > [1] [module Kernel - RDoc Documentation](http://ruby-doc.org/core/classes/Kernel.html#M001968)
> > 
> > Regards,  
> > Jordan
> > 
> > This thread may be of some help.

---

<div class="post-metadata">

### Author: ![aidy2](https://avatars.discourse-cdn.com/v4/letter/a/da6949/32.png) [@aidy2](https://rubytalk.org/u/aidy2)
#### Post date: [20 September 2006 13:50 UTC](https://rubytalk.org/t/self-method/31148/5 "2006-09-20T13:50:09Z")

</div>

> &nbsp;&nbsp;Based on that, you could try something as follows:
> 
> def who\_am\_i?  
> &nbsp;&nbsp;return caller[0]  
> end
> 
> def meth  
> &nbsp;&nbsp;p who\_am\_i?  
> end

Thank you, that certainly works

aidy

---

<div class="post-metadata">

### Author: ![Jordan\_Callicoat](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@Jordan\_Callicoat](https://rubytalk.org/u/Jordan_Callicoat)
#### Post date: [20 September 2006 14:00 UTC](https://rubytalk.org/t/self-method/31148/6 "2006-09-20T14:00:14Z")

</div>

Vincent Fourmond wrote:

> &nbsp;&nbsp;Based on that, you could try something as follows:
> 
> def who\_am\_i?  
> &nbsp;&nbsp;return caller[0]  
> end
> 
> def meth  
> &nbsp;&nbsp;p who\_am\_i?  
> end
> 
> meth
> 
> &nbsp;&nbsp;As it returns text, you'll need some regular expression handling, but  
> that could help you...
> 
> &nbsp;&nbsp;Vince, in a ruby mood today...

See! I told you that someone smarter than me would come up with a  
solution! Never say never unless you want to be proven wrong rather  
quickly! 😉

Using Vince's idea:

def who\_am\_i?  
&nbsp;&nbsp;caller[0].match(/`([^']+)'/)[1]  
end

def meth  
&nbsp;&nbsp;p who\_am\_i?  
end

meth # =\> "meth"

🙂

Regards,  
Jordan

---

<div class="post-metadata">

### Author: ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)
#### Post date: [20 September 2006 13:55 UTC](https://rubytalk.org/t/self-method/31148/7 "2006-09-20T13:55:08Z")

</div>

You can also do that without a helper method:

def meth  
&nbsp;&nbsp;&nbsp;p caller(0)[0]  
end

Note: default argument for caller is 1, which makes it start one level above.

Kind regards

&nbsp;&nbsp;robert

> **···**
>
> On 20.09.2006 15:48, aidy wrote:
> 
> > > &nbsp;&nbsp;Based on that, you could try something as follows:
> > > 
> > > def who\_am\_i?  
> > > &nbsp;&nbsp;return caller[0]  
> > > end
> > > 
> > > def meth  
> > > &nbsp;&nbsp;p who\_am\_i?  
> > > end
> > 
> > Thank you, that certainly works
