Skip to content

Ruby: a nice way to get name of current method you’re in:

2009 November 16

This is a nice extension to get the name of the method you’re currently in (Ruby < 1.9)

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

for Ruby >= 1.9 you can uses this:

[ruby]
module Kernel
private
def this_method
__method__
end
end
[/ruby]

[see Stack Over Flow as well]

Flattr this!

Comments are closed.