Ruby 2.7 and new features

Tram Ho

On December 25, Ruby 2.7 will be officially released. This is also the last version before we will come to Ruby 3.0 . In the process of working, Ruby really made the programmers feel comfortable, which it always aims to. So let’s take a look in this new version, Ruby will bring us more more.

Pattern Matching

This is a feature that I think will be the most worth waiting for in this Ruby 2.7 version. It reminds me of the Destructuring assignment feature in ES6, but Ruby does more than that. Basically Pattern Matching combines destructuring and specifying patterns . We will see the examples below to better understand:

We can also use partern matching with more complex data types:

Numbered Parameters

In Ruby’s default blocks, parameters can be yielded. Surely we are familiar with the code as follows:

However, in this new version, the parameters will be numbered, and we can rewrite the following:

This is an experimental feature, although shorter, but it also has to pay a price that the code will somewhat lose clarity.

Enumerable # tally

This is a new method that counts the number of occurrences of an element in the array and returns a hash with corresponding values.

And this will be the result returned

Enumerable # filter_map

As the name implies, this is a combination of two methods that we are familiar with in previous versions, select and map example, when you want to retrieve even numbers and then convert them to a string, We can do the following:

But if you want to be more concise, you can use:

Array # intersection

In fact, this is the alias of the & operator we have known before:

With intersection they will be rewritten as follows:

We will get the same result however using intersection will look clearer and more object oriented


This is a pretty interesting and useful new feature. Its working mechanism is to create an infinite set by defining the next element based on an earlier element:

The above is an example of using produce in the power of 2

Beginless Ranges

Another change, and one way to create half the infinite set, is to not use the values ​​for the ends of a Range . This is an experimental feature and is mainly used to compare or test an element in a given set:

A few other changes

  • Symbol#start_with? / Symbol#end_with? : these are the methods that have been used for String strings, with this new version we can use it for Symbol
  • When a method defines a numeric value as **nil , it will be understood that this method does not receive a keyword at all
  • The GC module will be improved to work more effectively in improving memory fragmentation
  • Thread enhancements make multithreading faster


Recently I have sent you some new features as well as changes in Ruby 2.7 version will be released in the near future. This is also the best time for us to look back on what Ruby has brought and prepare for a bigger change in Ruby 3.0

(The article has references to sources on the Internet)

Share the news now

Source : Viblo