[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Xeno Campanoli

6/11/2009 1:38:00 AM

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

3 Answers

matt_neuburg

6/11/2009 2:25:00 AM

0

Xeno Campanoli <xeno.campanoli@gmail.com> wrote:

> 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.

botp

6/11/2009 2:46:00 AM

0

On Thu, Jun 11, 2009 at 9:37 AM, Xeno Campanoli<xeno.campanoli@gmail.com> wrote:
> This would be handy with some of my class methods for composing exception
> messages...??
>

maybe you can call caller and it will tell you who you are
eg,

> def whoami
> caller[0]
>end
=> nil

> def m
> whoami
> end
=> nil

> m
=> "(irb):62:in `m'"
>

Gary Wright

6/11/2009 3:43:00 AM

0


On Jun 10, 2009, at 9:37 PM, Xeno Campanoli wrote:

> 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