enable(target: nil, target_line: nil, target_thread: nil) -> boolRuby 2.0.0 から[permalink][rdoc][edit]enable(target: nil, target_line: nil, target_thread: nil) { ... } -> object(?target: Method | UnboundMethod | RubyVM::InstructionSequence | Proc | nil, ?target_line: int?, ?target_thread: Thread | :default | nil) → bool[T] (?target: Method | UnboundMethod | RubyVM::InstructionSequence | Proc | nil, ?target_line: int?, ?target_thread: Thread | :default | nil) { () → T } → T-
self のトレースを有効にします。
実行前の TracePoint#enabled? を返します。(トレースが既に有効であった場合は true を返します。そうでなければ false を返します)
- [PARAM]
target: - Method、UnboundMethod、Proc、RubyVM::InstructionSequence のいずれかを指定します。指定すると、その内部で起きるイベントだけをフックします。
- [PARAM]
target_line: -
targetと組み合わせて指定します。その行番号の:lineイベントだけをフックします。 - [PARAM]
target_thread: -
指定したスレッドで起きるイベントだけをフックします。ブロックが与えられ、
targetもtarget_lineも指定されていない場合、デフォルトは現在のスレッド(Thread.current)になります。 - [EXCEPTION]
ArgumentError: -
targetを指定せずにtarget_lineだけを指定した場合、またはtarget_lineを指定したのに:lineイベントがフック対象に含まれていない場合に発生します。
p trace.enabled? # => false p trace.enable # => false (実行前の状態) # トレースが有効 p trace.enabled? # => true p trace.enable # => true (実行前の状態) # 引き続きトレースが有効ブロックが与えられた場合、ブロック内でのみトレースが有効になります。この場合はブロックの評価結果を返します。
例
例: target を指定p trace.enabled? # => false trace.enable do p trace.enabled? # => true end p trace.enabled? # => falsedef foo; 1; end def bar; 2; end tp = TracePoint.new(:call) {|t| puts "call #{t.method_id}" } tp.enable(target: method(:foo)) { foo; bar; 1 } # => 1 # 出力は "call foo" だけ(bar は対象外)[注意] イベントフックのためのメソッドにブロックの外側で参照した場合は RuntimeError が発生する事に注意してください。
例trace.enable { p trace.lineno } # ~> RuntimeError: access from outside[SEE_ALSO] TracePoint#disable, TracePoint#enabled?
- [PARAM]