Ruby中有没有办法在方法中找到调用方法名?
例如:
class Test
def self.foo
Fooz.bar
end
end
class Fooz
def self.bar
# get Test.foo or foo
end
end
Run Code Online (Sandbox Code Playgroud) 我正在使用Ruby on Rails 3.2.2,为了显示警告消息以用于开发目的,我logger.warn在我的代码中使用.我想检索运行它的方法名称,logger.warn以便将该方法名称输出到日志文件中.
class ClassName < ActiveRecord::Base
def method_name
# Note: This code doesn't work. It is just a sample.
logger.warn "I would like to retrieve and display the #{self.class.to_s}##{method_name}"
end
end
Run Code Online (Sandbox Code Playgroud)
在日志文件中我想看到:
我想检索并显示ClassName#method_name
可能吗?如果是这样,我该怎么做?