# Using the current method name within current method

**URL:** <https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657>\
**Category:** Ruby\
**Created:** [July 31, 2006, 5:37am UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657 "2006-07-31T05:37:17Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [July 31, 2006, 5:37am UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/1 "2006-07-31T05:37:17Z")

</div>

Is there a way to get the current method name within the current method?

def test\_method  
puts self.current\_method  
end

— output ----  
test\_method

Thanks,  
Matt

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [July 31, 2006, 5:46am UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/2 "2006-07-31T05:46:37Z")

</div>

On Jul 30, 2006, at 11:37 PM, Matthew Heidemann wrote:

> Is there a way to get the current method name within the current  
> method?
> 
> def test\_method  
> puts self.current\_method  
> end
> 
> — output ----  
> test\_method

In 1.9 (Ruby CVS HEAD) there is # **method** and # **callee** : http://  
[eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l90](http://eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l90)

– Daniel

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [July 31, 2006, 6:08am UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/3 "2006-07-31T06:08:22Z")

</div>

This is kindof a hack, but it seems to work:

def get\_mname  
caller[0]=~/`(.\*?)’/ # note the first quote is a backtick  
$1  
end

def dinosaur  
puts get\_mname  
end

dinosaur()  
=\> ‘dinosaur’

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [July 31, 2006, 7:44am UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/4 "2006-07-31T07:44:33Z")

</div>

If you wanted to modularize it just for the sake of it, you could do  
something similar to the following:

module CurrentMethodName  
def this\_method  
caller[0]=~/`(.\*?)’/  
$1  
end  
end

And, then, use it like this:

class Foo  
include CurrentMethodName  
def bar  
this\_method  
end  
end

Hope this cleans it up a bit for you. It’s a holdover until 1.9.

M.T.

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [July 31, 2006, 10:30am UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/5 "2006-07-31T10:30:07Z")

</div>

Matt T. wrote:

> If you wanted to modularize it just for the sake of it, you could do  
> something similar to the following:
> 
> module CurrentMethodName  
> def this\_method  
> caller[0]=~/`(.\*?)’/  
> $1  
> end  
> end

You can rewrite that as

def this\_method  
caller[0][/`([^’]\*)’/, 1]  
end

and thusly avoid the global variable.

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [August 1, 2006, 5:29am UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/6 "2006-08-01T05:29:05Z")

</div>

As much as I’d like to take credit for knowing its superior speed, I  
can’t as I stole it from Aleks. 🙂

However, I did know that $1 wasn’t a true global in the sense of the  
term, but a magic variable specific to the regexp test. It’s commonly  
acceptable, and, hey, if it’s faster, why not, right?

A side note: this is one of the first things I’ve worked with that I  
decided to make into something that was includable… It’s a  
milestone in my way of thinking and coding with Ruby! Yay! (Sorry, had  
to tell someone!)

Cheers!

M.T.

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [August 1, 2006, 1:49pm UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/7 "2006-08-01T13:49:17Z")

</div>

Logan C. wrote:

> > > end  
> > > Kind regards
> > 
> > ```
> > robert
> > 
> > ```
> 
> I have a sneaking suspicion he might have done it with the global  
> variable (although $1 isn’t really global) on purpose, since =~  
> /literal/ is the fastest way to do a regexp.

I did a benchmark just to put some meat to this:

13:37:39 [Temp]: ./bm.rb  
Rehearsal --------------------------------------------  
global 1.859000 0.000000 1.859000 ( 1.863000)  
String[] 2.156000 0.000000 2.156000 ( 2.180000)  
----------------------------------- total: 4.015000sec

```
            user system total real

```

global 1.828000 0.000000 1.828000 ( 1.872000)  
String[] 2.094000 0.000000 2.094000 ( 2.172000)

IOW, the global var version uses 87,3% of the time or is 15% faster.

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [July 31, 2006, 8:00pm UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/8 "2006-07-31T20:00:34Z")

</div>

On Jul 31, 2006, at 4:25 AM, Robert K. wrote:

> You can rewrite that as

I have a sneaking suspicion he might have done it with the global  
variable (although $1 isn’t really global) on purpose, since =~ /  
literal/ is the fastest way to do a regexp.

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [August 3, 2006, 2:24pm UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/9 "2006-08-03T14:24:47Z")

</div>

Aleks Kissinger wrote:

> Depending on how you feel about mucking with core ruby types, a method  
> like this one seems like a decent candidate for mixing in to Object.
> 
> class Object  
> def this\_method  
> caller[0]=~/`(.\*?)’/  
> $1  
> end  
> end

These methods are typically put into Kernel and made private. Also, you  
don’t check whether the RX actually matches. I’d also prefer to change  
the RX to a more efficient one. So that would give

module Kernel  
private  
def this\_method  
caller[0] =~ /`([^’]\*)’/ and $1  
end  
end

Kind regards

```
robert
```

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [August 3, 2006, 12:55pm UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/10 "2006-08-03T12:55:21Z")

</div>

Depending on how you feel about mucking with core ruby types, a method  
like this one seems like a decent candidate for mixing in to Object.

class Object  
def this\_method  
caller[0]=~/`(.\*?)’/  
$1  
end  
end

Course I’m one of those crazy irb hack loving people that mixes  
convenience methods into everything…

> > > Matt T. wrote:
> > > 
> > > > If you wanted to modularize it just for the sake of it, you could do  
> > > > something similar to the following:  
> > > > module CurrentMethodName  
> > > > def this\_method  
> > > > caller[0]=~/`(.\*?)’/  
> > > > $1  
> > > > end  
> > > > end

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [March 10, 2010, 4:21am UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/11 "2010-03-10T04:21:21Z")

</div>

Robert K. wrote:

> These methods are typically put into Kernel and made private. Also, you  
> don’t check whether the RX actually matches. I’d also prefer to change  
> the RX to a more efficient one. So that would give
> 
> module Kernel  
> private  
> def this\_method  
> caller[0] =~ /`([^’]\*)’/ and $1  
> end  
> end
> 
> Kind regards
> 
> robert

Excellent! I was searching for this for a long time. ‘what method called  
this method’ or ‘how to find out which method called this method ?’

So, here’s one:

module Kernel  
private  
def this\_method  
caller[0] =~ /`([^']*)'/ and $1 end def calling_method caller[1] =~ /`([^’]\*)’/ and $1  
end  
end

Example:

def a  
b  
end

def b  
puts "this method: " + this\_method  
puts "calling method: " + calling\_method  
end

calling ‘a’ will output  
this method: b  
calling method: a

Thx!

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [March 10, 2010, 10:00am UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/12 "2010-03-10T10:00:27Z")

</div>

2010/3/10 Ace S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > def this\_method  
> > private  
> > def a  
> > calling method: a  
> > Wow, you just reviewed a 3.5 year old thread! 🙂

Btw, nowadays I would change the regular expression matching to use  
String#[] which I find much more elegant:

def this\_method  
caller[0][/`([^‘]\*)’/, 1]  
end

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [August 3, 2006, 5:49pm UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/13 "2006-08-03T17:49:26Z")

</div>

I like that much better. That seems to fit in with the rest of how  
Ruby works. Well done. Also, thanks for improving the RX efficiency: I  
see how that would be a bit more efficient.

M.T.

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [March 10, 2010, 10:22am UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/14 "2010-03-10T10:22:42Z")

</div>

On Wed, Mar 10, 2010 at 9:29 AM, Robert K.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> 2010/3/10 Ace S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):  
> I would leave Kernel (and Object ) alone for that

module Whatever  
end

mix it in where and when you want. Ok that is a matter of taste but  
gives you a less intrusive way

Cheers  
R

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [March 22, 2011, 4:56am UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/15 "2011-03-22T04:56:05Z")

</div>

Reviving thread again: I’d use **method**

In terms of original example.

def test\_method  
puts **method**  
end

Maybe **method** didn’t exist in 2006 (too lazy to check, maybe its

> =1.8.7), in 2010 it definitely did 😉

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [November 11, 2012, 3:56am UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/16 "2012-11-11T03:56:14Z")

</div>

> Using caller does give me the name of the calling method, but there is  
> no reference to what module or class that method belonged to. Is that  
> possible to obtain?

def get\_mname  
caller[0]=~/`(.\*?)’/ # note the first quote is a backtick  
$1  
end

def dinosaur  
puts get\_mname  
puts method(get\_mname).owner  
end

dinosaur

–output:–  
dinosaur  
Object

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [November 10, 2012, 5:53pm UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/17 "2012-11-10T17:53:51Z")

</div>

eT Ma wrote in post #988662:

> Reviving thread again: I’d use **method**
> 
> In terms of original example.
> 
> def test\_method  
> puts **method**  
> end
> 
> Maybe **method** didn’t exist in 2006 (too lazy to check, maybe its
> 
> > =1.8.7), in 2010 it definitely did 😉

I’m writing plugins for SketchUp which uses Ruby 1.8.6. **method** does  
not exist there.

Using caller does give me the name of the calling method, but there is  
no reference to what module or class that method belonged to. Is that  
possible to obtain?

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [November 11, 2012, 9:46pm UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/18 "2012-11-11T21:46:38Z")

</div>

7stud – wrote in post #1083904:

> > Using caller does give me the name of the calling method, but there is  
> > no reference to what module or class that method belonged to. Is that  
> > possible to obtain?
> 
> def get\_mname  
> caller[0]=~/`(.\*?)’/ # note the first quote is a backtick  
> $1  
> end
> 
> def dinosaur  
> puts get\_mname  
> puts method(get\_mname).owner  
> end
> 
> dinosaur
> 
> –output:–  
> dinosaur  
> Object

There is no Method#owner in Ruby 1.8.6.

And I don’t understand how it would have worked anyway - determining the  
class/method a method belongs to given just a string with the method  
name? If you have two classes, Foo and Bar - each each method #biz - how  
would method(‘biz’).owner returned the correct caller?

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [May 16, 2013, 7:32pm UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/19 "2013-05-16T19:32:44Z")

</div>

7stud – wrote in post #1083904:

> > Using caller does give me the name of the calling method, but there is  
> > no reference to what module or class that method belonged to. Is that  
> > possible to obtain?
> 
> def get\_mname  
> caller[0]=~/`(.\*?)’/ # note the first quote is a backtick  
> $1  
> end
> 
> def dinosaur  
> puts get\_mname  
> puts method(get\_mname).owner  
> end
> 
> dinosaur
> 
> –output:–  
> dinosaur  
> Object

Guys, FYI caller doesn’t give meaningful data about method when the  
method is called from a bare file, e.g. a Cucumber Step definition.  
Instead of giving

common\_steps.rb:42:in `some\_method\_or\_other’ …

it gives

common\_steps.rb:42:in `block in \<top (required)\>’ …

For these situations you’re better off using **method** and  
self.name.to\_s if you also need the class in which the method is  
contained.

HTH,

Dermot

---

<div class="post-metadata">

**Author:** ![Matthew\_Heidemann](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/d9b06d/32.png) [@Matthew\_Heidemann](https://www.ruby-forum.com/u/Matthew_Heidemann)\
**Post date:** [May 28, 2013, 10:25am UTC](https://www.ruby-forum.com/t/using-the-current-method-name-within-current-method/100657/20 "2013-05-28T10:25:33Z")

</div>

On Thu, May 16, 2013 at 7:32 PM, Dermot C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Guys, FYI caller doesn’t give meaningful data about method when the  
> method is called from a bare file, e.g. a Cucumber Step definition.

What exactly distinguishes a “bare file” from ordinary ruby code?

> contained.  
> You could as well just look at the next item in the list and use that:

$ ruby -e ‘def f;p caller(1);end;f;1.times {f}’  
[“-e:1:in `<main>'"] ["-e:1:in `block in '”, “-e:1:in `times'", "-e:1:in `'”]

Kind regards

robert
