227

$0 is the variable for the top level Ruby program, but is there one for the current method?

1
  • One use is checking super can be called within a SimpleDelegator object: def description; __getobj__.respond_to?(__method__) ? super : 'No description'; end
    – Kris
    Commented Sep 26, 2013 at 13:57

5 Answers 5

388

Even better than my first answer you can use __method__:

class Foo
  def test_method
    __method__
  end
end

This returns a symbol – for example, :test_method. To return the method name as a string, call __method__.to_s instead.

Note: This requires Ruby 1.8.7.

2
  • 12
    the ':' is just the symbol symbol. :) just do __method__.to_s and it'll be the method name, nothing else
    – Lambart
    Commented Sep 25, 2013 at 23:39
  • What if I am in rake task? Commented Aug 26, 2017 at 8:27
39

Depending on what you actually want, you can use either __method__ or __callee__, which return the currently executing method's name as a symbol.

On ruby 1.9, both of them behave identically (as far as the docs and my testing are concerned).

On ruby 2.1 & 2.2 __callee__ behaves differently if you call an alias of the defined method. The docs for the two are different:

  • __method__: "the name at the definition of the current method" (i.e. the name as it was defined)
  • __callee__: "the called name of the current method" (i.e. the name as it was called (invoked))

Test script:

require 'pp'
puts RUBY_VERSION
class Foo
  def orig
    {callee: __callee__, method: __method__}
  end
  alias_method :myalias, :orig
end
pp( {call_orig: Foo.new.orig, call_alias: Foo.new.myalias} )

1.9.3 Output:

1.9.3
{:call_orig=>{:callee=>:orig, :method=>:orig},
 :call_alias=>{:callee=>:orig, :method=>:orig}}

2.1.2 Output (__callee__ returns the aliased name, but __method__ returns the name at the point the method was defined):

2.1.2
{:call_orig=>{:callee=>:orig, :method=>:orig},
 :call_alias=>{:callee=>:myalias, :method=>:orig}}
0
28

From http://snippets.dzone.com/posts/show/2785:

module Kernel
private
    def this_method_name
      caller[0] =~ /`([^']*)'/ and $1
    end
end

class Foo
  def test_method
    this_method_name
  end
end

puts Foo.new.test_method    # => test_method
3
  • 5
    This was very helpful to me for finding the name of the calling (as opposed to the current) method.
    – Lambart
    Commented Sep 27, 2013 at 0:50
  • Does __callee__ not do this? Commented May 22, 2015 at 16:32
  • Great solution Mark The best solution at now. Great job Commented Sep 19, 2016 at 8:13
13

For Ruby 1.9+ I'd recommend using __callee__

6
  • 3
    __callee__ behaves differently prior to 1.9, so it's best to stick with __method__ since it has consistent behavior. __callee__ behaves the same as __method__ after 1.9. Commented Apr 9, 2014 at 3:25
  • @LeighMcCulloch can you explain the difference with an example (possibly in a new answer)? Commented Sep 11, 2014 at 8:11
  • 1
    @CiroSantilli六四事件法轮功纳米比亚威视 def m1() puts("here is #{__method__} method. My caller is #{__callee__}.") end; def m2() puts("here is #{__method__} method. Let's call m1"); m1 end; m2 Don't you see anything strange ?
    – jgburet
    Commented Jul 8, 2015 at 11:20
  • 4
    @LeighMcCulloch actually now __callee__ and __method__ has different behaviour. See pastie.org/10380985 (ruby 2.1.5) Commented Aug 28, 2015 at 8:26
  • 1
    pastie.org is down. Forever or just now?
    – Nakilon
    Commented Mar 18, 2018 at 2:12
-3

I got the same issue to retrieve method name in view file. I got the solution by

params[:action] # it will return method's name

if you want to get controller's name then

params[:controller] # it will return you controller's name
2
  • 4
    I think you misinterpreted the question to be about rails controller actions & http methods... this answer should probably be deleted. Commented Jan 27, 2016 at 16:45
  • Useful for getting the name of the currently executing (controller) method from the view. Commented Jul 5, 2016 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.