本文简要介绍ruby语言中 Kernel.set_trace_func
的用法。
用法
set_trace_func(proc) → proc
set_trace_func(nil) → nil
Establishes _proc_ as the handler for tracing, or disables
tracing if the parameter is +nil+.
*Note:* this method is obsolete, please use TracePoint instead.
_proc_ takes up to six parameters:
* an event name
* a filename
* a line number
* an object id
* a binding
* the name of a class
_proc_ is invoked whenever an event occurs.
Events are:
+c-call+:: call a C-language routine
+c-return+:: return from a C-language routine
+call+:: call a Ruby method
+class+:: start a class or module definition
+end+:: finish a class or module definition
+line+:: execute code on a new line
+raise+:: raise an exception
+return+:: return from a Ruby method
Tracing is disabled within the context of _proc_.
class Test
def test
a = 1
b = 2
end
end
set_trace_func proc { |event, file, line, id, binding, classname|
printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
}
t = Test.new
t.test
line prog.rb:11 false
c-call prog.rb:11 new Class
c-call prog.rb:11 initialize Object
c-return prog.rb:11 initialize Object
c-return prog.rb:11 new Class
line prog.rb:12 false
call prog.rb:2 test Test
line prog.rb:3 test Test
line prog.rb:4 test Test
return prog.rb:4 test Test
请注意,对于 c-call
和 c-return
事件,返回的绑定是调用 C 方法的最近 Ruby 方法的绑定,因为 C 方法本身没有绑定。
相关用法
- Ruby Kernel.select用法及代码示例
- Ruby Kernel.syscall用法及代码示例
- Ruby Kernel.sprintf用法及代码示例
- Ruby Kernel.srand用法及代码示例
- Ruby Kernel.system用法及代码示例
- Ruby Kernel.sleep用法及代码示例
- Ruby Kernel.spawn用法及代码示例
- Ruby Kernel.local_variables用法及代码示例
- Ruby Kernel.Integer用法及代码示例
- Ruby Kernel.binding用法及代码示例
- Ruby Kernel.frozen?用法及代码示例
- Ruby Kernel.`cmd`用法及代码示例
- Ruby Kernel.autoload用法及代码示例
- Ruby Kernel.loop用法及代码示例
- Ruby Kernel.Hash用法及代码示例
- Ruby Kernel.caller用法及代码示例
- Ruby Kernel.exit!用法及代码示例
- Ruby Kernel.trap用法及代码示例
- Ruby Kernel.String用法及代码示例
- Ruby Kernel.then用法及代码示例
- Ruby Kernel.Pathname用法及代码示例
- Ruby Kernel.yield_self用法及代码示例
- Ruby Kernel.BigDecimal用法及代码示例
- Ruby Kernel.raise用法及代码示例
- Ruby Kernel.test用法及代码示例
注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Kernel.set_trace_func。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。