$0
变量是顶级Ruby程序的变量,但是对于当前的方法是否有变量呢?
发布于 2018-01-08 13:24:07
可以尝试下这个:
class Foo
def test_method
__method__
end
end
这将返回一个符号
发布于 2018-01-08 14:06:19
参考:http://nippets.dzone.com/post/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
https://stackoverflow.com/questions/-100001332
复制相似问题