New features introduced in Ruby 2.7

Tram Ho

Ruby is constantly growing

Version 2.7 revolves around changes to new features and methods.

Expected release of the revised version on December 25, 2019

According to Matz (Yukihiro Matsumoto – the creator of Ruby), this is the last version 2.x before we welcome version 3.0 next year

Before waiting for the release of 2.7, let’s find out some of the changes and features that have been publicized in this version !!!

1.Enumerable # tally

This is a new Ruby method, which counts all the elements in an array and returns a hash with their count.

You can do this yourself, but this tally method saves you the work.

For example:

It is very useful, isn’t it?

Explore more

2.Numbered Parameters For Blocks (Experimental)

This is an interesting new feature, it is a default name for the parameters in a block .

For example with a simple block :

|n| As a parameter, how can you define and use it?

But what if we give them default names?

It’s one of the features that Ruby 2.7 brings, albeit an experimental feature, which can save us a lot of code.

For example:

With _1 being the first parameter, we can also use _2 for the second parameter and _3 for the third parameter.

Note that there will be no value of _0

Previously, Matz had intended to name the above variable @1 , but since it looked quite similar to the instance variable, after discussion, it was replaced with _1

3.Array # intersection

New method but not a new function

It is like being an alias

Ruby 2.6 previously introduced the Array’s #union & #difference methods, in order to accommodate the brevity #| and # - .

But it lacks the #intersection method, we only know it with a shorter syntax that is & (find the same elements between 2 arrays)

For example

Explore more

4.Enumerable # filter_map

filter_map is a combination of filter and map methods (As the name implies)

You can do this in 2 ways:

We are inclined toward the first way, because it is explicitly easier to visualize

And for now, ruby ​​2.7 will do it with filter_map

Simple is not it

Besides, there are a few things you should know

That is filter_map is not the same as map + compact , because it will remove both false object and compact does not

5.Enumerator # produce

This is an interesting new method, but it requires some of your creativity if you want to make the most of your skin

What is Enumerator#produce ???

As suggested by the feature:

“This method produces an infinite sequence where each next element is calculated by applying the block to the previous element.”

Meaning it will create an infinite sequence with no stops

For example:

In the above example, it doesn’t matter if you want to get 10 take(10) or 10000 take(10000) elements

1 is its initialization value

And &:next is the method called to generate the next element in the string

6.IRB Gets a Face Lift?

Recently, IRB has received a lot of attention, and Ruby 2.7 continues to add it.

We’re getting:

We will have:

  • Multi-line editing
  • Write multi-line syntax (Multi-line editing)
  • Syntax Highlighting
  • Integrate more Ruby Documents
  • Command history is on by default
  • Auto-completion is enabled by default

This is a big change, it is so large that a message will appear if you start installing irb with Ruby 2.7.

7.Ruby Pattern Matching (Experimental)

A feature that is always required and it came in version 2.7 named Pattern Matching

It is marked as experimental.

As far as I understand, pattern matching same as regular expressions , but for data structures (Array / Hash).

For example:

If the match succeeds, the variable names a will become local, and you can access it.

Example with hash:

Keyword is in

It’s easy to understand, isn’t it !!

8. Summary

Above are the latest updates on the Ruby programming language

Hopefully this article will give everyone an overview before the 2.7 release

Thanks for watching


Share the news now

Source : Viblo