Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

TracePoint.new

(*::_ToSym events) { (instance tp) -> void } -> instance
TracePoint.new(*events) { |tp| block } → tp

Returns a new TracePoint object, not enabled by default.

To activate the TracePoint object, use TracePoint#enable:

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

trace.enable  #=> false

puts "Hello, TracePoint!"
# ...
# [48, IRB::Notifier::AbstractNotifier, :printf, :call]
# ...

To deactivate the trace, use TracePoint#disable.

Example
trace.disable

See Events at TracePoint for possible events and more information.

A block must be given; otherwise, an ArgumentError is raised.

If the trace method isn’t supported for the given event(s) filter, a RuntimeError is raised.

Example
TracePoint.trace(:line) do |tp|
  p tp.raised_exception
end
#=> RuntimeError: 'raised_exception' not supported by this event

If the trace method is called outside a block, a RuntimeError is raised.

Example
TracePoint.trace(:line) do |tp|
  $tp = tp
end
$tp.lineno #=> access from outside (RuntimeError)

Access from other ractors, threads or fibers is forbidden. TracePoints are active per-ractor so if you enable a TracePoint in one ractor, other ractors will not be affected.

See also .allow_reentry .stat .trace also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0