Ruby 4.0

A week from today, on Christmas (Th Dec 25, 2025), Matz will be releasing Ruby 4.0, the next major version. The article below reviews some of its new features.

The new method that I would use to replace existing code is Array#rfind.

Here’s the example they use.

# Ruby old
[2, 2, 3, 4, 6, 7, 8].reverse_each.find(&:odd?) => 7

# Ruby new
[2, 2, 3, 4, 6, 7, 8].rfind(&:odd?) => 7 

I would love to see this in Crystal too.

IMO, adding the three arguments #start, #end and #from_end to all sequence methods (when applicable) would be more useful.

Related: Strided subranges for `Array#[]` and `Range`-accepting methods · Issue #12390 · crystal-lang/crystal · GitHub

So it would be something like: [2, 2, 3, 4, 5, 6, 7, 8].find.from_end(&:odd?)?

[2, 2, 3, 4, 5, 6, 7, 8].find(from_end: true, &:odd?) rather.

Wow! That’s really (too) verbose. I like [2, 2, 3, 4, 5, 6, 7, 8].rfind(&:odd?) better.

Despite their verbosiness (?) I find a common set of parameters more intuitive and useful than having different methods for different options. And where is #rreduce btw?

2 Likes

Wawoo, I left Ruby several years(since about 3.1?), it’s about to release version 4.0 soon.

To commemorate Ruby’s 30 years, and eminent release of Ruby 4.0, it’s newly designed website was released.

5 Likes

I wonder why. I am sure reverse_each.find optimizes that, so it is not slower than rfind (that’s why the _each part is on there). So it is just to save characters typing? Maybe they should have called it dnif and save another character.