introspection/binding_implicit_parameters_no_parens.rb

tests/_examples/introspection/binding_implicit_parameters_no_parens.rb in the metorex repository.

# A Binding taken in a block reports the implicit parameters that block
# takes, `it` or `_1` to `_9`, and not those of a block around it.
reported = proc { _2; binding.implicit_parameters }.call :north, :south
p reported
read = proc { it; binding.implicit_parameter_get :it }.call :east
p read
defined = proc { _1; binding.implicit_parameter_defined? :_1 }.call :west
p defined
defined = proc { binding.implicit_parameter_defined? :it }.call :west
p defined
nested = proc { _1; proc { binding.implicit_parameters }.call }.call :up
p nested

begin
  proc { binding.implicit_parameter_get :_1 }.call
rescue NameError => problem
  p problem.message.start_with? "implicit parameter '_1' is not defined for"
end
begin
  binding.implicit_parameter_get :total
rescue NameError => problem
  p problem.message
end

# A Symbol spelled like a numbered parameter is a Symbol, and a block that
# names one takes no parameter.
arity = proc { :_1 }.arity
p arity
picked = [3, 4].map { false ? :_1 : _1 }
p picked

Output:

[:_1, :_2]
:east
true
false
[]
true
"'total' is not an implicit parameter"
0
[3, 4]