2010/3/10 Ace Suares <ace / suares.an>:
> Robert Klemme wrote:
>
> <snip>
>
>> These methods are typically put into Kernel and made private. =A0Also, y=
ou
>> don't check whether the RX actually matches. =A0I'd also prefer to chang=
e
>> the RX to a more efficient one. =A0So that would give
>>
>> module Kernel
>> private
>> =A0 =A0def this_method
>> =A0 =A0 =A0caller[0] =3D~ /`([^']*)'/ and $1
>> =A0 =A0end
>> end
>
> Excellent! I was searching for this for a long time. 'what method called
> this method' or 'how to find out which method called this method ?'
>
> So, here's one:
>
> module Kernel
> private
> =A0def this_method
> =A0 =A0caller[0] =3D~ /`([^']*)'/ and $1
> =A0end
> =A0def calling_method
> =A0 =A0caller[1] =3D~ /`([^']*)'/ and $1
> =A0end
> end
>
> Example:
>
> def a
> =A0b
> end
>
> def b
> =A0puts "this method: " + this_method
> =A0puts "calling method: " + calling_method
> end
>
> calling 'a' will output
> this method: b
> calling method: a

Wow, you just reviewed a 3.5 year old thread! :-)

Btw, nowadays I would change the regular expression matching to use
String#[] which I find much more elegant:

  def this_method
    caller[0][/`([^']*)'/, 1]
  end

Kind regards

robert

--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/