Skip to content

Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →

class method

TracePoint.new

TracePoint.new(*events) { |obj| block } → obj

Returns a new TracePoint object, not enabled by default.

Next, in order to activate the trace, you must 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]
# ...

When you want to deactivate the trace, you must 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 included in the given events 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 block, a RuntimeError is raised.

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

Access from other threads is also forbidden.

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