# Method name

**URL:** <https://www.ruby-forum.com/t/method-name/94905>\
**Category:** Ruby\
**Created:** [May 17, 2006, 2:05pm UTC](https://www.ruby-forum.com/t/method-name/94905 "2006-05-17T14:05:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [May 17, 2006, 2:05pm UTC](https://www.ruby-forum.com/t/method-name/94905/1 "2006-05-17T14:05:21Z")

</div>

Hi,

I’m looking around, but I didn’t find a replacement for c++  
**PRETTY\_FUNCTION** or **FUNC** macro.

How can I find out a method’s name at runtime?

Best regards  
Michael

---

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [May 17, 2006, 2:36pm UTC](https://www.ruby-forum.com/t/method-name/94905/2 "2006-05-17T14:36:09Z")

</div>

On 5/17/06, Michael [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi,
> 
> I’m looking around, but I didn’t find a replacement for c++  
> **PRETTY\_FUNCTION** or **FUNC** macro.
> 
> How can I find out a method’s name at runtime?

I assume you mean the name of the current method. Here’s one way.

name = caller(0).first.split.last

This gives it to you with single quotes around it which you may want  
to strip off.

---

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [May 17, 2006, 5:37pm UTC](https://www.ruby-forum.com/t/method-name/94905/3 "2006-05-17T17:37:41Z")

</div>

Mark V. wrote:

> name = caller(0).first.split.last
> 
> This gives it to you with single quotes around it which you may want  
> to strip off.

Does this work?

def **FUNCTION**  
caller(1).first[/ `(.\*)’\Z/, 1]  
end

Cheers,  
Dave

---

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [May 17, 2006, 7:41pm UTC](https://www.ruby-forum.com/t/method-name/94905/4 "2006-05-17T19:41:46Z")

</div>

Dave B. wrote:

> Mark V. wrote:
> 
> > name = caller(0).first.split.last
> > 
> > This gives it to you with single quotes around it which you may want  
> > to strip off.
> 
> Does this work?
> 
> def **FUNCTION**  
> caller(1).first[/ `(.\*)’\Z/, 1]  
> end
> 
> Cheers,  
> Dave

What if I am at the outmost level?  
I cannot get a function name , just got something like this:  
test.rb:2546

It may not be a perfect solution.

---

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [May 17, 2006, 8:42pm UTC](https://www.ruby-forum.com/t/method-name/94905/5 "2006-05-17T20:42:12Z")

</div>

uncutstone wu wrote:

> Dave B. wrote:
> 
> > Mark V. wrote:
> > 
> > > name = caller(0).first.split.last
> > > 
> > > This gives it to you with single quotes around it which you may want  
> > > to strip off.
> > 
> > Does this work?
> > 
> > def **FUNCTION**  
> > caller(1).first[/ `(.\*)’\Z/, 1]  
> > end
> > 
> > Cheers,  
> > Dave
> 
> What if I am at the outmost level?  
> I cannot get a function name , just got something like this:  
> test.rb:2546
> 
> It may not be a perfect solution.

It seems to work properly for me on the outermost level.

def **FUNCTION**  
caller(1).first[/ `(.\*)’\Z/, 1]  
end

def test\_method  
puts “i’m in ‘#{ **FUNCTION** }’”  
end

test\_method  
puts “outer: i’m in ‘#{ **FUNCTION** }’”

produces,

i’m in ‘test\_method’  
outer: i’m in ‘’

---

<div class="post-metadata">

**Author:** ![michael](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/4bbf92/32.png) [@michael](https://www.ruby-forum.com/u/michael)\
**Post date:** [May 17, 2006, 9:00pm UTC](https://www.ruby-forum.com/t/method-name/94905/6 "2006-05-17T21:00:00Z")

</div>

Mike N. wrote:

> i’m in ‘test\_method’  
> outer: i’m in ‘’

I mean at the outmost level, you are still execute in a function’s  
context, but you cannot get this function’s name.
