# Reflection to get method name from within method?

**URL:** <https://www.ruby-forum.com/t/reflection-to-get-method-name-from-within-method/97857>\
**Category:** Ruby\
**Created:** [July 10, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/reflection-to-get-method-name-from-within-method/97857 "2006-07-10T23:18:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ezra\_z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f07891/32.png) [@ezra\_z](https://www.ruby-forum.com/u/ezra_z)\
**Post date:** [July 10, 2006, 11:18pm UTC](https://www.ruby-forum.com/t/reflection-to-get-method-name-from-within-method/97857/1 "2006-07-10T23:18:51Z")

</div>

Friends-

```
Is there anyway to use reflection or someother technique to get the

```

name of a method from within the method when it is running?

So if I had this:

def foo  
#some magick incantation here that returns the name of the method  
“foo”  
end

Thanks-  
-Ezra

---

<div class="post-metadata">

**Author:** ![ezra\_z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f07891/32.png) [@ezra\_z](https://www.ruby-forum.com/u/ezra_z)\
**Post date:** [July 10, 2006, 11:34pm UTC](https://www.ruby-forum.com/t/reflection-to-get-method-name-from-within-method/97857/2 "2006-07-10T23:34:28Z")

</div>

On 7/10/06, Ezra Z. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> end
> 
> Thanks-  
> -Ezra

You could use something like this:

def method\_called  
caller[0].match(/`([^']+)/).captures[0]  
end

def test\_me  
p method\_called  
end

test\_me  
#=\> “test\_me”

I don’t know of any other way (unless perhaps you can get this info  
via ParseTree or evil.rb).

Regards,  
Sean

---

<div class="post-metadata">

**Author:** ![ezra\_z](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/f07891/32.png) [@ezra\_z](https://www.ruby-forum.com/u/ezra_z)\
**Post date:** [July 10, 2006, 11:50pm UTC](https://www.ruby-forum.com/t/reflection-to-get-method-name-from-within-method/97857/3 "2006-07-10T23:50:27Z")

</div>

On Jul 10, 2006, at 2:31 PM, Sean O’Halpin wrote:

> > #some magick incantation here that returns the name of the method  
> > caller[0].match(/`([^’]+)/).captures[0]  
> > via ParseTree or evil.rb).
> 
> Regards,  
> Sean

Ok thanks Sean, that will work for what I need to do.

Cheers-  
-Ezra
