如何检索当前方法名称以便将其输出到记录器文件?

use*_*882 6 ruby methods

我正在使用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

可能吗?如果是这样,我该怎么做?

Bhu*_*dha 23

class ClassName < ActiveRecord::Base
  def method_name
    logger.warn("I would like to retrieve and display the #{self.class}##{__method__}")
  end
end
Run Code Online (Sandbox Code Playgroud)

这应该做的工作.

  • `.to_s`调用是多余的.插值将自动调用`to_s`. (2认同)