So $0 is the env variable for the top level Ruby program ... but is there one for the current method?
+6
A:
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
Mark A. Nicolosi
2008-10-14 00:21:38
+12
A:
Even better than my first answer you can use __method__:
class Foo
def test_method
__method__
end
end
This includes a : before the name which you can easily remove.
Note: This requires Ruby 1.8.7.
Mark A. Nicolosi
2008-10-14 00:24:59
in `foo': undefined local variable or method `__method__' for main:Object (NameError)
Kent Fredric
2008-10-14 00:30:58
Sorry, this requires Ruby 1.9... I'll update my post.
Mark A. Nicolosi
2008-10-14 00:33:21
Make that 1.8.7: http://www.ruby-doc.org/core-1.8.7/classes/Kernel.src/M001072.html
Mark A. Nicolosi
2008-10-14 00:36:32
just my luck, still on 1.8.6 :)
Kent Fredric
2008-10-14 00:37:35