Get the name of the currently executing method

salt.racer picture salt.racer · Oct 14, 2008 · Viewed 63k times · Source

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

Answer

Mark A. Nicolosi picture Mark A. Nicolosi · Oct 14, 2008

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.