Reflection to get method name from within method?

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

On 7/10/06, Ezra Z. [email protected] 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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs