trace_var(p1, p2 = v2)
public
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'
Show source
VALUE
rb_f_trace_var(int argc, VALUE *argv)
{
VALUE var, cmd;
struct global_entry *entry;
struct trace_var *trace;
rb_secure(4);
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;
}