instance method
Binding#local_variable_get
(::interned varname) -> untyped
binding.local_variable_get(symbol) → obj
proc.c
View on GitHub ↗
static VALUE
bind_local_variable_get(VALUE bindval, VALUE sym)
{
ID lid = check_local_id(bindval, &sym);
const rb_binding_t *bind;
const VALUE *ptr;
const rb_env_t *env;
if (!lid) goto undefined;
if (rb_numparam_id_p(lid)) {
rb_name_err_raise("numbered parameter '%1$s' is not a local variable",
bindval, ID2SYM(lid));
}
GetBindingPtr(bindval, bind);
env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
if ((ptr = get_local_variable_ptr(&env, lid, TRUE)) != NULL) {
return *ptr;
}
sym = ID2SYM(lid);
undefined:
rb_name_err_raise("local variable '%1$s' is not defined for %2$s",
bindval, sym);
UNREACHABLE_RETURN(Qundef);
}
Returns the value of the local variable symbol.
Example
def foo a = 1 binding.local_variable_get(:a) #=> 1 binding.local_variable_get(:b) #=> NameError end
This method is the short version of the following code:
Example
binding.eval("#{symbol}")
Ruby version changes
-
4.0
-
Binding#local_variablesdoes no longer include numbered parameters. Also,Binding#local_variable_get,Binding#local_variable_set, andBinding#local_variable_defined?reject to handle numbered parameters. [Bug #21049]
-
See also
#local_variable_defined?
#local_variables
#implicit_parameters
#local_variable_set
also in
4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0