TracePoint#enable
# File tmp/rubies/ruby-4.0.0/trace_point.rb, line 261
def enable(target: nil, target_line: nil, target_thread: :default)
Primitive.attr! :use_block
Primitive.tracepoint_enable_m(target, target_line, target_thread)
end
Activates the trace.
Returns true if the trace was enabled. Returns false if the trace was disabled.
trace.enabled? #=> false trace.enable #=> false (previous state) # trace is enabled trace.enabled? #=> true trace.enable #=> true (previous state) # trace is still enabled
If a block is given, the trace will only be enabled during the block execution. If target and target_line are both nil, then target_thread will default to the current thread if a block is given.
trace.enabled? #=> false trace.enable do trace.enabled? # Only enabled for this block and thread end trace.enabled? #=> false
The target, target_line, and target_thread parameters are used to limit tracing to specified code objects. target should be a code object for which RubyVM::InstructionSequence.of will return an instruction sequence.
t = TracePoint.new(:line) { |tp| p tp } def m1 p 1 end def m2 p 2 end t.enable(target: method(:m1)) m1 # Prints #<TracePoint:line test.rb:4 in `m1'> m2 # Prints nothing
Note: You cannot access event hooks within the enable block.
trace.enable { p tp.lineno } #=> RuntimeError: access from outside
Ruby version changes
-
3.2
-
TracePoint#enabletarget_threadkeyword argument now defaults to the current thread if a block is given andtargetandtarget_linekeyword arguments are not passed. [Bug #16889]
-