Self.method?

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

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. :wink: See also Kernel#caller
[1]

[1] module Kernel - RDoc Documentation

Regards,
Jordan

Hello !

In short, no it is not possible (in an easy way) -- but just wait, and
someone smarter than me will prove me wrong. :wink: 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...

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/205118

Michael Guterl

···

On 9/20/06, MonkeeSage <MonkeeSage@gmail.com> wrote:

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. :wink: See also Kernel#caller
[1]

[1] module Kernel - RDoc Documentation

Regards,
Jordan

This thread may be of some help.

  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

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! :wink:

Using Vince's idea:

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

def meth
  p who_am_i?
end

meth # => "meth"

:slight_smile:

Regards,
Jordan

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

···

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