0

How to get the specific name of the current method in Ruby?

Example:

class Pages
  def home
    @title = self.name
  end
end

I want to get this result:

p = Pages.new
p.home # it must print "home"
joelparkerhenderson
  • 34,808
  • 19
  • 98
  • 119
Yudians
  • 13
  • 1

1 Answers1

3

Well, What you're looking for is the variable "__method__" which should return a symbol representing the current method. That variable is in scope whenever you are inside a method.

To get the variable as a string: __method__.to_s

Joe Daniels
  • 1,646
  • 1
  • 11
  • 9