instance method · added in Ruby 4.0
Binding#implicit_parameter_get
binding.implicit_parameter_get(symbol) → obj
proc.c
View on GitHub ↗
static VALUE
bind_implicit_parameter_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 == idIt) lid = idItImplicit;
if (!lid || !rb_implicit_param_p(lid)) {
rb_name_err_raise("'%1$s' is not an implicit parameter",
bindval, sym);
}
GetBindingPtr(bindval, bind);
env = VM_ENV_ENVVAL_PTR(vm_block_ep(&bind->block));
if ((ptr = get_local_variable_ptr(&env, lid, FALSE)) != NULL) {
return *ptr;
}
if (lid == idItImplicit) lid = idIt;
rb_name_err_raise("implicit parameter '%1$s' is not defined for %2$s", bindval, ID2SYM(lid));
UNREACHABLE_RETURN(Qundef);
}
Returns the value of the numbered parameter or “it” parameter.
Example
def foo [42].each do it binding.implicit_parameter_get(:it) #=> 42 end { k: 42 }.each do _2 binding.implicit_parameter_get(:_1) #=> :k binding.implicit_parameter_get(:_2) #=> 42 end end
Ruby version changes
-
4.0
-
Binding#implicit_parameters,Binding#implicit_parameter_get, andBinding#implicit_parameter_defined?have been added to access numbered parameters and “it” parameter. [Bug #21049]
-
See also
#implicit_parameter_defined?
#implicit_parameters
#eval
#local_variable_defined?
also in
4.1