Forum: Ruby using the current method name within current method

Posted by Matthew Heidemann (Guest)
on 2006-07-31 05:37
(Received via mailing list)
Is there a way to get the current method name within the current method?

def test_method
  puts self.current_method
end

--- output ----
test_method

Thanks,
Matt
Posted by Daniel Harple (Guest)
on 2006-07-31 05:46
(Received via mailing list)
On Jul 30, 2006, at 11:37 PM, Matthew Heidemann wrote:

> Is there a way to get the current method name within the current  
> method?
>
> def test_method
>  puts self.current_method
> end
>
> --- output ----
> test_method

In 1.9 (Ruby CVS HEAD) there is #__method__ and #__callee__: http://
eigenclass.org/hiki.rb?Changes+in+Ruby+1.9#l90

-- Daniel
Posted by Aleks Kissinger (Guest)
on 2006-07-31 06:08
(Received via mailing list)
This is kindof a hack, but it seems to work:

def get_mname
  caller[0]=~/`(.*?)'/  # note the first quote is a backtick
  $1
end

def dinosaur
  puts get_mname
end

dinosaur()
=> 'dinosaur'
Posted by Matt Todd (Guest)
on 2006-07-31 07:44
(Received via mailing list)
If you wanted to modularize it just for the sake of it, you could do
something similar to the following:

module CurrentMethodName
  def this_method
    caller[0]=~/`(.*?)'/
    $1
  end
end

And, then, use it like this:

class Foo
  include CurrentMethodName
  def bar
    this_method
  end
end

Hope this cleans it up a bit for you. It's a holdover until 1.9.

M.T.
Posted by Robert Klemme (Guest)
on 2006-07-31 10:30
(Received via mailing list)
Matt Todd wrote:
> If you wanted to modularize it just for the sake of it, you could do
> something similar to the following:
> 
> module CurrentMethodName
>  def this_method
>    caller[0]=~/`(.*?)'/
>    $1
>  end
> end

You can rewrite that as

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

and thusly avoid the global variable.

Kind regards

	robert
Posted by Logan Capaldo (Guest)
on 2006-07-31 20:00
(Received via mailing list)
On Jul 31, 2006, at 4:25 AM, Robert Klemme wrote:

> You can rewrite that as
>
I have a sneaking suspicion he might have done it with the global
variable (although $1 isn't really global) on purpose, since =~ /
literal/ is the fastest way to do a regexp.
Posted by Matt Todd (Guest)
on 2006-08-01 05:29
(Received via mailing list)
As much as I'd like to take credit for knowing its superior speed, I
can't as I stole it from Aleks. :)

However, I did know that $1 wasn't a true global in the sense of the
term, but a magic variable specific to the regexp test. It's commonly
acceptable, and, hey, if it's faster, why not, right?

A side note: this is one of the first things I've worked with that I
decided to make into something that was includable.... It's a
milestone in my way of thinking and coding with Ruby! Yay! (Sorry, had
to tell someone!)

Cheers!

M.T.
Posted by Robert Klemme (Guest)
on 2006-08-01 13:49
(Received via mailing list)
Logan Capaldo wrote:
>>>  end
>> Kind regards
>>
>>     robert
>>
> 
> I have a sneaking suspicion he might have done it with the global 
> variable (although $1 isn't really global) on purpose, since =~ 
> /literal/ is the fastest way to do a regexp.

I did a benchmark just to put some meat to this:

13:37:39 [Temp]: ./bm.rb
Rehearsal --------------------------------------------
global     1.859000   0.000000   1.859000 (  1.863000)
String[]   2.156000   0.000000   2.156000 (  2.180000)
----------------------------------- total: 4.015000sec

                user     system      total        real
global     1.828000   0.000000   1.828000 (  1.872000)
String[]   2.094000   0.000000   2.094000 (  2.172000)

IOW, the global var version uses 87,3% of the time or is 15% faster.

Kind regards

	robert
Posted by Aleks Kissinger (Guest)
on 2006-08-03 12:55
(Received via mailing list)
Depending on how you feel about mucking with core ruby types, a method
like this one seems like a decent candidate for mixing in to Object.

class Object
  def this_method
    caller[0]=~/`(.*?)'/
    $1
  end
end

Course I'm one of those crazy irb hack loving people that mixes
convenience methods into everything....

> >> Matt Todd wrote:
> >>> If you wanted to modularize it just for the sake of it, you could do
> >>> something similar to the following:
> >>> module CurrentMethodName
> >>>  def this_method
> >>>    caller[0]=~/`(.*?)'/
> >>>    $1
> >>>  end
> >>> end

<snip>
Posted by Robert Klemme (Guest)
on 2006-08-03 14:24
(Received via mailing list)
Aleks Kissinger wrote:
> Depending on how you feel about mucking with core ruby types, a method
> like this one seems like a decent candidate for mixing in to Object.
> 
> class Object
>  def this_method
>    caller[0]=~/`(.*?)'/
>    $1
>  end
> end

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

Kind regards

	robert
Posted by Matt Todd (Guest)
on 2006-08-03 17:49
(Received via mailing list)
I like that much better. That seems to fit in with the rest of how
Ruby works. Well done. Also, thanks for improving the RX efficiency: I
see how that would be a bit more efficient.

M.T.
Posted by Ace Suares (acesuares)
on 2010-03-10 04:21
Robert Klemme wrote:

<snip>

> 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
> 
> Kind regards
> 
>   robert

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

Thx!



Posted by Robert Klemme (Guest)
on 2010-03-10 10:00
(Received via mailing list)
2010/3/10 Ace Suares <ace@suares.an>:
>>    def this_method
> private
> def a
> 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
Posted by Robert Dober (Guest)
on 2010-03-10 10:22
(Received via mailing list)
On Wed, Mar 10, 2010 at 9:29 AM, Robert Klemme
<shortcutter@googlemail.com> wrote:
> 2010/3/10 Ace Suares <ace@suares.an>:
I would leave Kernel (and Object ) alone for that

module Whatever
end

mix it in where and when you want. Ok that is a matter of taste but
gives you a less intrusive way

Cheers
R
Posted by eT Ma (etm)
on 2011-03-22 04:56
Reviving thread again: I'd use __method__

In terms of original example.

def test_method
  puts __method__
end

Maybe __method__ didn't exist in 2006 (too lazy to check, maybe its
>=1.8.7), in 2010 it definitely did ;-)
Posted by Thomas Thomassen (thomthom)
on 2012-11-10 17:53
eT Ma wrote in post #988662:
> Reviving thread again: I'd use __method__
>
> In terms of original example.
>
> def test_method
>   puts __method__
> end
>
> Maybe __method__ didn't exist in 2006 (too lazy to check, maybe its
>>=1.8.7), in 2010 it definitely did ;-)

I'm writing plugins for SketchUp which uses Ruby 1.8.6. __method__ does 
not exist there.

Using caller does give me the name of the calling method, but there is 
no reference to what module or class that method belonged to. Is that 
possible to obtain?
Posted by 7stud -- (7stud)
on 2012-11-11 03:56
> Using caller does give me the name of the calling method, but there is
> no reference to what module or class that method belonged to. Is that
> possible to obtain?

def get_mname
  caller[0]=~/`(.*?)'/  # note the first quote is a backtick
  $1
end

def dinosaur
  puts get_mname
  puts method(get_mname).owner
end

dinosaur

--output:--
dinosaur
Object
Posted by Thomas Thomassen (thomthom)
on 2012-11-11 21:46
7stud -- wrote in post #1083904:
>> Using caller does give me the name of the calling method, but there is
>> no reference to what module or class that method belonged to. Is that
>> possible to obtain?
>
> def get_mname
>   caller[0]=~/`(.*?)'/  # note the first quote is a backtick
>   $1
> end
>
> def dinosaur
>   puts get_mname
>   puts method(get_mname).owner
> end
>
> dinosaur
>
> --output:--
> dinosaur
> Object

There is no Method#owner in Ruby 1.8.6.

And I don't understand how it would have worked anyway - determining the 
class/method a method belongs to given just a string with the method 
name? If you have two classes, Foo and Bar - each each method #biz - how 
would method('biz').owner returned the correct caller?
Posted by Dermot C. (dermot_c)
on 2013-05-16 19:32
7stud -- wrote in post #1083904:
>> Using caller does give me the name of the calling method, but there is
>> no reference to what module or class that method belonged to. Is that
>> possible to obtain?
>
> def get_mname
>   caller[0]=~/`(.*?)'/  # note the first quote is a backtick
>   $1
> end
>
> def dinosaur
>   puts get_mname
>   puts method(get_mname).owner
> end
>
> dinosaur
>
> --output:--
> dinosaur
> Object

Guys, FYI caller doesn't give meaningful data about method when the
method is called from a bare file, e.g. a Cucumber Step definition.
Instead of giving

common_steps.rb:42:in `some_method_or_other' ...

it gives

common_steps.rb:42:in `block in <top (required)>' ...

For these situations you're better off using __method__ and
self.name.to_s if you also need the class in which the method is 
contained.

HTH,

Dermot
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.