获取当前执行方法的名称 [英] Get the name of the currently executing method

查看:130
本文介绍了获取当前执行方法的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$0是顶级Ruby程序的变量,但是当前方法是否存在该变量?

$0 is the variable for the top level Ruby program, but is there one for the current method?

推荐答案

比我的第一个答案还好,您可以使用__method __:

Even better than my first answer you can use __method__:

class Foo
  def test_method
    __method__
  end
end

这将返回一个符号-例如:test_method.要以字符串形式返回方法名称,请调用__method__.to_s.

This returns a symbol – for example, :test_method. To return the method name as a string, call __method__.to_s instead.

注意:这需要Ruby 1.8.7.

Note: This requires Ruby 1.8.7.

这篇关于获取当前执行方法的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆