---
title: "Ruby 2.7 adds numbered parameters as default block parameters"
description:
  "Ruby 2.7 introduces numbered parameters as default block parameters"
canonical_url: "https://www.bigbinary.com/blog/ruby-2-7-introduces-numbered-parameters-as-default-block-parameters"
markdown_url: "https://www.bigbinary.com/blog/ruby-2-7-introduces-numbered-parameters-as-default-block-parameters.md"
---

# Ruby 2.7 adds numbered parameters as default block parameters

Ruby 2.7 introduces numbered parameters as default block parameters

- Author: Taha Husain
- Published: March 3, 2020
- Categories: Ruby 2.7, Ruby

At some point, all of us have used names like `a`, `n`, `i` etc for block
parameters. Below are few examples where numbered parameters can come in handy.

```ruby

> (1..10).each { |n| p n * 3 }

> { a: [1, 2, 3], b: [2, 4, 6], c: [3, 6, 9] }.each { |_k, v| p v }

> [10, 100, 1000].each_with_index { |n, i| p n, i }

```

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 `n` or `i`
etc, 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.

```ruby

> (1..10).each { p _1 * 3 }

> { a: [1, 2, 3], b: [2, 4, 6], c: [3, 6, 9] }.each { p _2 }

> [10, 100, 1000].each_with_index { p _1, _2 }

```

Like mentioned in
[News-2.7.0 docs](https://github.com/ruby/ruby/blob/7f6bd6bb1c2220d2d7c17b77abf52fb4af548001/doc/NEWS-2.7.0#numbered-parameters),
Ruby now raises a warning if we try to define local variable in the format `_1`.
Local variable will have precedence over numbered parameter inside the block.

```ruby

> _1 = 0
> => warning: `_1' is reserved for numbered parameter; consider another name

> [10].each { p _1 }
> => 0

```

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.

```ruby

> ["a", "b", "c"].each_with_index { |alphabet, index| p _1, _2}

=> SyntaxError ((irb):1: ordinary parameter is defined)

```

This feature was suggested 9 years back and came back in discussion last year.
After many suggestions community agreed to use `_1` syntax.

Head to following links to read the discussion behind numbered parameters,
[Feature #4475](https://bugs.ruby-lang.org/issues/4475) and
[Discussion #15723](https://bugs.ruby-lang.org/issues/15723).

Here's relevant
[commit](https://github.com/ruby/ruby/commit/12acc751e3e7fd6f8aec33abf661724ad76c862a)
for this feature.

## Links

- [Human page](https://www.bigbinary.com/blog/ruby-2-7-introduces-numbered-parameters-as-default-block-parameters)
