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"
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"
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