What’s new in Ruby 2.7?

Guy Maliar
Dec 2 · 6 min read

With Rubyconf 2019 behind us and Ruby 2.7 releasing this December, it’s the perfect time to go through what are the new features in the newest 2.x Ruby version and also the last release before 3.0.

What defines this version of Ruby is a mix of quality-of-life improvements such as Symbol#start_with? and Enumerable#filter_map, big yet often controversial features such as pattern matching and numbered arguments and bug fixes.

What also makes this version special is that it is the last one before Ruby 3.0 that is due will be released in Dec 2020 and has been presented by Matz in this year’s Rubyconf keynotes (https://www.youtube.com/watch?v=2g9R7PUCEXo&list=PLE7tQUdRKcyZDE8nFrKaqkpd-XK4huygU&index=2&t=0s) and covered by my fellow team member, Snir David, in post https://medium.com/@snird/rubyconf-2019-main-takeaways-from-the-keynote-ruby-3-0-and-the-road-ahead-b17b42cc50be.


Pattern Matching

The best one line definition would be case/when + multiple assignments.

A more complete definition would be multiple patterns which some arbitrary data are checked against (case/when) and deconstructing the data according to those patterns (multiple assignments).

The best place to learn about pattern matching is probably the Ruby test files.

Pattern matching checks for the structure of the data so that it can be partially deconstructed as well,

Much more information is available in the following slides which I highly recommend reading.

Numbered Parameters

Numbered parameter as the default block parameter is introduced as an experimental feature, it allows us to drop the pipe definition in blocks, (|value|) and create shorter blocks with numbered parameters _1, _2, _3 etc.

  • Defining a local variable named _1 is still honored when present, but renders a warning

Some concerns have been issued by the community about how clear and/or how needed this feature actually is.

From the clarity perspective it’s quite unclear (no pun intended) what is actually better.

I’ve thought of and found some examples of what can we do with it.

Argument Forwarding

A nice quality of life feature when we want to pass all the arguments as is,

All arguments to foo are forwarded to bar, including keyword and block arguments.

Beginless Ranges

An experimental feature with still with some concerns, beginless ranges are looked more as a feature for DSL completeness rather than usability. This feature is used mostly for comparison where we’d like to define a condition for “everything below the specified value”. Dates and numbers make the perfect example and even for version strings.

Flip flop operator is back

A lesser known feature in Ruby is the flip flop operator. It has it’s root coming from Perl but isn’t used as much anymore.

The flip flop operator is a range (..) operator used between two conditions in a conditional statement. It evaluates to false until the first condition evaluates to true and stays true until the second condition evaluates to true.

Array#intersection

The equivalent to:

Can now be written as:

Enumerable#filter_map

select and map are often used to transform an array while filtering some items out completely.

Up until now it would have been possible with

filter_map allows us to pair the two together and solve the problem faster.

Enumerable#tally

The way to count the amount of repetitions of a specific value in an Enumerable would have been one of the followings:

As of 2.7 we can instead use tally :

Enumerator.produce

Enumerator.produce(initial, &block), will produce an infinite sequence where each next element is calculated by applying block to previous.

Or for pagination (taken directly from https://bugs.ruby-lang.org/issues/14781)

A very short and well explanation is available here.

Symbol#start_with? / Symbol#end_with?

Just some convenience methods that behave like String#start_with? and String#end_with?

Keyword Arguments spec changes

Keyword arguments assignments spec is changing a little bit with additional warnings and deprecation warnings that will probably be hapenning next year with Ruby 3.

Taken directly from the Ruby NEWS,

  • When a method call passes a Hash at the last argument, and when it passes no keywords, and when the called method accepts keywords, a warning is emitted. To continue treating as keywords, add a double splat operator to avoid the warning and ensure correct behavior in Ruby 3.
  • When a method call passes keywords to a method that accepts keywords, but it does not pass enough required positional arguments, the keywords are treated as a final required positional argument, and a warning is emitted. Pass the argument as a hash instead of keywords to avoid the warning and ensure correct behavior in Ruby 3.
  • When a method accepts specific keywords but not a keyword splat, and a hash or keywords splat is passed to the method that includes both Symbol and non-Symbol keys, the hash will continue to be split, and a warning will be emitted. You will need to update the calling code to pass separate hashes to ensure correct behavior in Ruby 3.
  • If a method does not accept keywords, and is called with keywords, the keywords are still treated as a positional hash, with no warning. This behavior will continue to work in Ruby 3.
  • Accept non-symbols keyword arguments if method accepts arbitrary keywords

Most of the examples here are either taken from my imagination or from the Ruby NEWS file, Ruby Issue Tracking System or other blog posts, if there’s any credit missing please let me know.

Ruby Inside

Ruby articles and posts

Guy Maliar

Written by

Director of Technology @ Tailor Brands | Google Developer Expert

Ruby Inside

Ruby articles and posts

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade