# Name of method?

**URL:** <https://www.ruby-forum.com/t/name-of-method/181755>\
**Category:** Ruby\
**Created:** [January 17, 2010, 11:30pm UTC](https://www.ruby-forum.com/t/name-of-method/181755 "2010-01-17T23:30:12Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 17, 2010, 11:30pm UTC](https://www.ruby-forum.com/t/name-of-method/181755/1 "2010-01-17T23:30:12Z")

</div>

How do I get the name of a method?

def xxx  
@WhatsMyName = ???  
end

What should ??? be? I want it to return “xxx”.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 17, 2010, 11:38pm UTC](https://www.ruby-forum.com/t/name-of-method/181755/2 "2010-01-17T23:38:40Z")

</div>

Ralph S. wrote:

> How do I get the name of a method?
> 
> def xxx  
> @WhatsMyName = ???  
> end
> 
> What should ??? be? I want it to return “xxx”.

Then ??? should be “xxx”. No shortcut here, I think.

Also, @WhatsMyName is a poor variable name. You want @whats\_my\_name or  
(better) @name.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 17, 2010, 11:44pm UTC](https://www.ruby-forum.com/t/name-of-method/181755/3 "2010-01-17T23:44:50Z")

</div>

Ralph S. wrote:

> How do I get the name of a method?
> 
> def xxx  
> @WhatsMyName = ???  
> end
> 
> What should ??? be? I want it to return “xxx”.

I should mention that Method#name would work if you could get a hold of  
the Method object, but I don’t see how you’d do that from within the  
method definition.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 18, 2010, 1:00am UTC](https://www.ruby-forum.com/t/name-of-method/181755/4 "2010-01-18T01:00:55Z")

</div>

On Sun, Jan 17, 2010 at 5:29 PM, Ralph S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> How do I get the name of a method?
> 
> def xxx  
> @WhatsMyName = ???  
> end
> 
> What should ??? be? I want it to return “xxx”.

Here’s a bit of a hack using caller.

def method\_name  
caller.first.match(/in `(.\*)'/)[1]  
end

class Foo  
def bar  
puts method\_name  
end  
alias\_method :baz, :bar  
end

Foo.new.bar  
Foo.new.baz

outputs:  
bar  
baz

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [Rick DeNatale - Developer - IBM | LinkedIn](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 18, 2010, 1:37am UTC](https://www.ruby-forum.com/t/name-of-method/181755/5 "2010-01-18T01:37:00Z")

</div>

Gary W. wrote:

> On Jan 17, 2010, at 5:29 PM, Ralph S. wrote:
> 
> > How do I get the name of a method?
> > 
> > def xxx  
> > @WhatsMyName = ???  
> > end
> 
> In Ruby 1.9:
> 
> def foo  
> puts “this is method: #{ **method** }”  
> end
> 
> You can also use **callee**.
> 
> Gary W.

Cool! Didn’t know about that.

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 18, 2010, 2:01am UTC](https://www.ruby-forum.com/t/name-of-method/181755/6 "2010-01-18T02:01:15Z")

</div>

GW\> On Jan 17, 2010, at 5:29 PM, Ralph S. wrote:

> > How do I get the name of a method?

> > def xxx  
> > @WhatsMyName = ???  
> > end

GW\> In Ruby 1.9:

GW\> def foo  
GW\> puts “this is method: #{ **method** }”  
GW\> end

GW\> You can also use **callee**.

Ok … The Pickaxe book warns about not going to 1.9 because of library  
issues.

Is it safe to upgrade to 1.9 now? I see that the one-click installer is  
still a release candidate.

Is there a more-or-less central place that discusses 1.9 vis-a-vis  
compatibility with all the gems and libraries and drop-ins that one  
might be using?

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 18, 2010, 1:17am UTC](https://www.ruby-forum.com/t/name-of-method/181755/7 "2010-01-18T01:17:56Z")

</div>

On Jan 17, 2010, at 5:29 PM, Ralph S. wrote:

> How do I get the name of a method?
> 
> def xxx  
> @WhatsMyName = ???  
> end

In Ruby 1.9:

def foo  
puts “this is method: #{ **method** }”  
end

You can also use **callee**.

Gary W.

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 18, 2010, 6:03am UTC](https://www.ruby-forum.com/t/name-of-method/181755/8 "2010-01-18T06:03:35Z")

</div>

Ralph S. wrote:

> > >
> 
> Is it safe to upgrade to 1.9 now? I see that the one-click installer is still a release candidate.
> 
> Is there a more-or-less central place that discusses 1.9 vis-a-vis compatibility with all the gems and libraries and drop-ins that one might be using?

I would say to use 1.9 unless it does not work for you.

You can check some compatibility information at [http://isitruby19.com/](http://isitruby19.com/)

-Justin

---

<div class="post-metadata">

**Author:** ![ralphshnelvar](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e19adc/32.png) [@ralphshnelvar](https://www.ruby-forum.com/u/ralphshnelvar)\
**Post date:** [January 18, 2010, 3:43pm UTC](https://www.ruby-forum.com/t/name-of-method/181755/9 "2010-01-18T15:43:09Z")

</div>

Justin C. wrote:

> Ralph S. wrote:
> 
> > > >
> > 
> > Is it safe to upgrade to 1.9 now? I see that the one-click installer is still a release candidate.
> > 
> > Is there a more-or-less central place that discusses 1.9 vis-a-vis compatibility with all the gems and libraries and drop-ins that one might be using?
> 
> I would say to use 1.9 unless it does not work for you.
> 
> You can check some compatibility information at [http://isitruby19.com/](http://isitruby19.com/)

I would not use 1.9 with Rails yet, but some people do. My one  
non-Rails R. project is actually JRuby, so I don’t quite have that  
option.

> -Justin

Best,  
–Â  
Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
