Method: Binding#local_variable_set
- Defined in:
- proc.c
#local_variable_set(symbol, obj) ⇒ Object
Set local variable named symbol as obj.
def foo a = 1 bind = binding bind.local_variable_set(:a, 2) # set existing local variable ‘a’ bind.local_variable_set(:b, 3) # create new local variable ‘b’ # ‘b’ exists only in binding
p bind.local_variable_get(:a) #=> 2 p bind.local_variable_get(:b) #=> 3 p a #=> 2 p b #=> NameError end
This method behaves similarly to the following code:
binding.eval("#{symbol} = #{obj}")
if obj can be dumped in Ruby code.
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 |
# File 'proc.c', line 572
static VALUE
bind_local_variable_set(VALUE bindval, VALUE sym, VALUE val)
{
ID lid = check_local_id(bindval, &sym);
rb_binding_t *bind;
const VALUE *ptr;
const rb_env_t *env;
if (!lid) lid = rb_intern_str(sym);
GetBindingPtr(bindval, bind);
env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
if ((ptr = get_local_variable_ptr(&env, lid)) == NULL) {
/* not found. create new env */
ptr = rb_binding_add_dynavars(bindval, bind, 1, &lid);
env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
}
#if YJIT_STATS
rb_yjit_collect_binding_set();
#endif
RB_OBJ_WRITE(env, ptr, val);
return val;
}
|