What's new in Ruby 3.4

Ruby 3.4 brings exciting enhancements to one of the most beloved programming languages. Let's explore the key features that make this release particularly interesting for seasoned developers.

The New 'it' variable: simplified block parameters

Ruby 3.4 introduces a game-changing feature with the new 'it' variable. This addition builds upon the numbered block parameters introduced in Ruby 2.7, offering an even more elegant solution for single-parameter blocks. Consider this clean and expressive chain of operations:

username='kamil-d'
username.gsub("-", "")
    .then { URI.parse("https://sampleyourpage.com/?q=firstname:#{it}") }
    .then { Net::HTTP.get(it) }
    .then { JSON.parse(it) }

This syntax significantly improves readability compared to traditional approaches. For array transformations, you can now write:

arr = ["one", "two", "three"]
# before
arr.map{|item| item.upcase}
# after
arr.map { it.upcase } # => ["ONE", "TWO", "THREE"]

[1, 2, 3].each { it*2 } # => [2, 4, 6]

[1, 2, 3].each { puts it * 2 } # 2 4 6

Chilled Strings: A Step Towards Immutability

Ruby 3.4 introduces "chilled strings" - a middle ground between mutable and frozen strings. Without the frozen_string_literal comment, string literals are now "chilled," meaning they can be mutated but will trigger warnings. This feature helps developers identify potential string mutations and encourages immutable string practices.

mytext = ""
mytext << "abc" # warning: literal string will be frozen in the future

mytext = +"" # make that string 'not chilled'
mytext << "abc" # no warning

Enhanced Range Operations

Range operations have received notable improvements. The overlap? method provides an elegant way to check range intersections:

(1..3).overlap?(1..2) # true
(..5).reverse_each.take(3) #=> [5, 4, 3]

Summary

Ruby 3.4 focuses on developer ergonomics with the introduction of the 'it' variable, takes steps towards safer string handling with chilled strings, and enhances range operations. These features demonstrate Ruby's commitment to both elegance and practicality.
Of course we have more new things like: new modular garbage collector, YJIT improvements, Prism as default parser, but I tried to focus on more code changes.

Happy Rubying!