开发者

How to get current method in Rails 3?

开发者 https://www.devze.com 2023-03-14 05:25 出处:网络
I found the following code, which I guess goes in config/initializers/kernel.rb. module Kernel private def this_method

I found the following code, which I guess goes in config/initializers/kernel.rb.

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

For adding to the log, is this the preferred way to get the current method?

Thanks.开发者_运维知识库


That seems like a decent way to get the calling method and give you the ability to call this_method in your code to add to the log.

If you are using Ruby 1.9.2 you can call __method__ instead and not worry about defining a special method to do so.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号