From: Ezra Zygmuntowicz Date: 2006-07-11T06:47:25+09:00 Subject: Re: Reflection to get method name from within method? On Jul 10, 2006, at 2:31 PM, Sean O'Halpin wrote: > On 7/10/06, Ezra Zygmuntowicz wrote: >> 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 >> > 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 > Ok thanks Sean, that will work for what I need to do. Cheers- -Ezra