Skip to content

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

instance method

Kernel#trace_var

(::interned name, ::String | ::Kernel::_Tracer cmd) -> nil
(::interned name) { (untyped value) -> void } -> nil
(::interned name, nil) -> ::Array[::String | ::Kernel::_Tracer]?
trace_var(symbol, cmd ) → nil
trace_var(symbol) {|val| block } → nil

Controls tracing of assignments to global variables. The parameter symbol identifies the variable (as either a string name or a symbol identifier). cmd (which may be a string or a Proc object) or block is executed whenever the variable is assigned. The block or Proc object receives the variable’s new value as a parameter. Also see untrace_var.

Example
trace_var :$_, proc {|v| puts "$_ is now '#{v}'" }
$_ = "hello"
$_ = ' there'

produces:

$_ is now 'hello'
$_ is now ' there'
See also #throw #trap #then #untrace_var also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0