[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

self.method?

aidy

9/20/2006 11:04:00 AM

Hi,

We can write "self.class" to get the current object, but is it possible
to do something like a "self.method" to retrieve the currently running
method?

Cheers

Aidy

5 Answers

MonkeeSage

9/20/2006 12:17:00 PM

0

aidy wrote:
> We can write "self.class" to get the current object, but is it possible
> to do something like a "self.method" to retrieve the currently running
> method?

Hi Aidy,

In short, no it is not possible (in an easy way) -- but just wait, and
someone smarter than me will prove me wrong. ;) See also Kernel#caller
[1]

[1] http://ruby-doc.org/core/classes/Kernel.ht...

Regards,
Jordan

Vincent Fourmond

9/20/2006 1:13:00 PM

0


Hello !
>
> In short, no it is not possible (in an easy way) -- but just wait, and
> someone smarter than me will prove me wrong. ;) See also Kernel#caller
> [1]

Based on that, you could try something as follows:

def who_am_i?
return caller[0]
end

def meth
p who_am_i?
end

meth

As it returns text, you'll need some regular expression handling, but
that could help you...

Vince, in a ruby mood today...

aidy

9/20/2006 1:48:00 PM

0

>
> Based on that, you could try something as follows:
>
> def who_am_i?
> return caller[0]
> end
>
> def meth
> p who_am_i?
> end
>

Thank you, that certainly works

aidy

Robert Klemme

9/20/2006 1:54:00 PM

0

On 20.09.2006 15:48, aidy wrote:
>> Based on that, you could try something as follows:
>>
>> def who_am_i?
>> return caller[0]
>> end
>>
>> def meth
>> p who_am_i?
>> end
>>
>
> Thank you, that certainly works

You can also do that without a helper method:

def meth
p caller(0)[0]
end

Note: default argument for caller is 1, which makes it start one level
above.

Kind regards

robert

MonkeeSage

9/20/2006 1:57:00 PM

0

Vincent Fourmond wrote:
> Based on that, you could try something as follows:
>
> def who_am_i?
> return caller[0]
> end
>
> def meth
> p who_am_i?
> end
>
> meth
>
> As it returns text, you'll need some regular expression handling, but
> that could help you...
>
> Vince, in a ruby mood today...

See! I told you that someone smarter than me would come up with a
solution! Never say never unless you want to be proven wrong rather
quickly! ;)

Using Vince's idea:

def who_am_i?
caller[0].match(/`([^']+)'/)[1]
end

def meth
p who_am_i?
end

meth # => "meth"

:)

Regards,
Jordan