Is there a method to tell me the name of the method I'm in?

X

Xeno Campanoli

This would be handy with some of my class methods for composing exception
messages...??
 
M

Matt Neuburg

Xeno Campanoli said:
This would be handy with some of my class methods for composing exception
messages...??

I think the standard thing to do is to munge Exception's backtrace:

def other_method
raise
end
def what_method
other_method
end
begin
what_method
rescue
puts $!.backtrace[0].split(" ")[1][1..-2]
end

At least that is the sort of thing I have been resorting to; if there is
a better way I'd like to know about it! :) m.
 
G

Gary Wright

This would be handy with some of my class methods for composing
exception messages...??

It Ruby 1.9, __callee__ (or __method__) will return the name of the
current method as a symbol.

Gary Wright
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top