2014-03-14 30 views
0

當我通過函數進行輕鬆調試時,Ruby/Rails是否可以自動打印函數名稱?正如我發現在每個功能之前添加投入都很麻煩。請指教和感謝!當遍歷函數時自動打印每個函數名稱

實施例:

def update_original_calendar 
    puts '-> update_original_calendar' 
end 

def update_destination_calendar 
    puts '-> update_destination_calendar' 
    update_original_calendar 
end 

實施例的結果:

-> update_destination_calendar 
-> update_original_calendar 
+1

這不是調試器是什麼對於? –

回答

0

你可以使用Kernel#set_trace_func方法。將下面的代碼放在初始化邏輯中的某處。只要確保當應用程序邏輯達到你想要跟蹤的方法時,那段代碼就已經被執行了。

classes_to_trace = %w(YOUR_CLASSNAME1 YOUR_CLASSNAME2) 
set_trace_func proc {|event, file, line, id, binding, classname| 
    if event == 'call' and classes_to_trace.include?(classname.to_s) 
    puts "event = #{event}, file = #{file}, line = #{line}, id = #{id}, classname = #{classname}" 
    end 
} 
0

如果您在使用Ruby 2.X:

http://ruby-doc.org/core-2.0/TracePoint.html

trace = TracePoint.new(:call) do |tp| 
    p [tp.lineno, tp.defined_class, tp.method_id, tp.event] 
end 

trace.enable 

# Your code here 

我沒有測試它,但它應該工作