2013-02-25 72 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 
相關問題