At some point, all of us have used names like
for block parameters.
Below are few examples where
numbered parameters can come in handy.
Ruby 2.7 introduces a new way to access block parameters.
Ruby 2.7 onwards, if block parameters are obvious and
we wish to not use absurd names like
we can use numbered parameters which are available
inside a block by default.
We can use _1 for first parameter, _2 for second parameter and so on.
Here’s how Ruby 2.7 provides numbered parameters inside a block. Below shown are the examples from above, only this time using numbered parameters.
Like mentioned in
Ruby now raises a warning if we try to
define local variable in the format
Local variable will have precedence over
numbered parameter inside the block.
Numbered parameters are not accessible inside the block
if we define ordinary parameters.
If we try to access
_1 when ordinary parameters are
defined, then ruby raises
SyntaxError like shown below.
This feature was suggested 9 years back
and came back in discussion last year.
After many suggestions community agreed to use
Here’s relevant commit for this feature.