Method: Kernel#trace_var
- Defined in:
- variable.c
#trace_var(symbol, cmd) ⇒ nil #trace_var(symbol) {|val| ... } ⇒ 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 Kernel::untrace_var.
trace_var :$_, proc {|v| puts "$_ is now '#{v}'" }
$_ = "hello"
$_ = ' there'
produces:
$_ is now 'hello'
$_ is now ' there'
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 |
# File 'variable.c', line 653 VALUE rb_f_trace_var(int argc, const VALUE *argv) { VALUE var, cmd; struct global_entry *entry; struct trace_var *trace; if (rb_scan_args(argc, argv, "11", &var, &cmd) == 1) { cmd = rb_block_proc(); } if (NIL_P(cmd)) { return rb_f_untrace_var(argc, argv); } entry = rb_global_entry(rb_to_id(var)); if (OBJ_TAINTED(cmd)) { rb_raise(rb_eSecurityError, "Insecure: tainted variable trace"); } trace = ALLOC(struct trace_var); trace->next = entry->var->trace; trace->func = rb_trace_eval; trace->data = cmd; trace->removed = 0; entry->var->trace = trace; return Qnil; } |