2013-02-25 69 views
0

在我写的一种方法中,我的代码如何知道它当前使用哪种方法?我需要这个,因为我想获取方法名并解析它以获得其中的一部分,如“add_order”,然后我可以使用第二部分“order”进行处理。如何知道我目前使用哪种方法

+2

复制方法的名称:http://stackoverflow.com/questions/199527/get-the-name-of-the-currently-executing -method-in-ruby http://stackoverflow.com/questions/5100299/how-to-get-the-name-of-the-calling-method http://stackoverflow.com/questions/10467625/how-to -retrieve-the-current-method-name-so-to-output-it-to-the-logger-file – halfelf 2013-02-25 07:52:49

+0

看到这个:http://stackoverflow.com/questions/199527/get-the-name-of-当前正在执行的方法在红宝石 – kikuchiyo 2013-02-25 07:54:27

+0

@halfelf,一个用于获取来电者的方法名称,而不是你目前在 – 2013-02-25 07:55:31

回答

5

使用__method__让您目前在

+0

你是对的。我刚刚找到它! http://ruby-doc.org/core-1.9.3/Kernel.html#method-i-__method__ – Juguang 2013-02-25 08:08:21

0
def get_mname 
    caller[0]=~/`(.*?)'/ # quote is a backtick 
    $1 
end 

def name_of_my_method 
    puts get_mname 
end 

name_of_my_method 

# => name_of_my_method 
相关问题