From: Robert Klemme Date: 2010-03-10T17:29:29+09:00 Subject: Re: using the current method name within current method 2010/3/10 Ace Suares : > Robert Klemme wrote: > > > >> These methods are typically put into Kernel and made private.  Also, you >> don't check whether the RX actually matches.  I'd also prefer to change >> the RX to a more efficient one.  So that would give >> >> module Kernel >> private >>    def this_method >>      caller[0] =~ /`([^']*)'/ and $1 >>    end >> 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 >  def this_method >    caller[0] =~ /`([^']*)'/ and $1 >  end >  def calling_method >    caller[1] =~ /`([^']*)'/ and $1 >  end > end > > Example: > > def a >  b > end > > def b >  puts "this method: " + this_method >  puts "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 -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/