Ruby 3.4's `it` Parameter: Cleaner Block Syntax for Ruby Developers

Ruby 3.4 introduces the it parameter as a cleaner alternative to numbered parameters like _1. This feature addresses readability concerns while maintaining the same performance characteristics.

Before Ruby 3.4

We had two main approaches for simple block operations:

1
2
3
4
5
6
7
# Traditional explicit parameter
users.map { |user| user.email.downcase }
posts.select { |post| post.published? }

# Ruby 2.7+ numbered parameters
users.map { _1.email.downcase }
posts.select { _1.published? }

The numbered parameter syntax, while concise, created cognitive overhead. As one Ruby core developer noted: “I’m not clever enough to remember the order of parameters.”

Ruby 3.4: The it Parameter

Ruby 3.4 introduces it as an implicit reference to the first block parameter.

Important: it works exclusively with single-argument blocks and cannot be mixed with numbered parameters:

1
2
3
4
5
6
7
8
# Using the new "it" parameter
users.map { it.email.downcase }
posts.select { it.published? }
orders.reject { it.total < 100 }

# This will error - cannot mix "it" with numbered parameters
hash.each { puts "#{it}: #{_2}" }
# SyntaxError: numbered parameters are not allowed when 'it' is already used

The it parameter provides the same performance as numbered parameters while improving readability for single-argument blocks.

Ruby Bug Tracker Discussion

The it parameter feature sparked significant discussion in Ruby bug tracker issue #18980. Key concerns centered around potential conflicts with existing code, particularly RSpec’s it method.

The Ruby core team addressed these concerns by implementing a gradual rollout:

  • Ruby 3.3: Warning system for potential conflicts
  • Ruby 3.4: Full implementation with new semantics

As Matz said: “ruby 3.3 will warn and ruby 3.4 will use the new semantics.” The primary motivation was reducing cognitive overhead, with one developer noting: “If you use it, it kinda implies there’s only a single argument, so you don’t need to spend time remembering whether _2 exists or not.”

Community feedback was mixed, with developers expressing various perspectives on readability and potential confusion. However, the feature was ultimately accepted due to its clarity for single-argument blocks.

Testing Framework Compatibility

The most common concern involves conflicts with Minitest’s it method. However, conflicts are minimal due to Ruby’s precedence rules:

1
2
3
4
5
6
7
8
9
10
11
# Minitest usage - works fine (has arguments)
describe "User" do
  it "creates a user" do
    # test code
  end
end

# No conflicts in practice - precedence rules prevent issues
it "processes arrays" do
  [1, 2, 3].each { puts it }  # Uses block parameter
end

Key Limitations

  • Single parameter only: Cannot be used with multiple-argument blocks
  • Cannot mix: it and numbered parameters cannot be combined
  • Parsing sensitivity: it +1 is parsed as it(+1), use it + 1

Safe to Use

The it parameter is safe to adopt because naked it calls (without arguments) are rare in practice. Most existing code that uses it as a method name includes arguments or receivers, preventing conflicts.

Ruby 3.3’s warning system helps identify potential issues before upgrading to 3.4, ensuring a smooth transition.

Ruby 3.4’s it parameter offers us a readable, performance-equivalent alternative to numbered parameters when working with single-argument blocks.

Prateek Choudhary
Prateek Choudhary
Technology Leader