shlogg · Early preview
Jetthoughts Dev @jetthoughts

Ruby 2.7+ Simplifies Block Parameters With _1, _2, And It

Ruby 2.7 introduced numbered params `_1`, `_2` for blocks, now in Ruby 3.4 use `it` to refer to current element being iterated over, more concise code!

In the Ruby programming language, it is possible to use default block parameters instead of variables. This feature allows for more concise and expressive code.

  
  
  Before Ruby 2.7

We would need to write code like this:

irb(main):005> [1, 2, 3].map { |el| el }
=> [1, 2, 3]

    
    

    
    




  
  
  From Ruby 2.7

However, in Ruby 2.7, numbered parameters were introduced, which allows us to write code like this:

irb(main):010> [10, 100, 1000].each_with_index { p "#{_1}, #{_2}" }
"10, 0"
"100, 1"
"1000, 2"

    
    

    
    




In the above example, _1 refers to the first ele...