What's new in Ruby 2.7.0

camfilho profile image Carlos Augusto M. Filho ・3 min read

After some controversial decisions between Matz and the community, Ruby 2.7.0 finally came out right on time to our Christmas be complete. This is the last release before the exciting Ruby 3.0 coming up in December of 2020.

Ruby 2.7.0 also begins to prepare all the Rubyists to the major changes that it is going through on the next year's release by showing warnings, experimental features and so forth. Here I'm going to present you some of the changes that 2.7.0 introduced.

Numbered Parameters

Now we can access the arguments passed to a block without having to define the block-argument between pipes ||.

[1, 2, 3].select { _1 > 2 } #=> [3]

As pointed here, sometimes the array name is so self-explanatory that we don't really need a block variable name to make it more readable. e.g. Books.all.each {|b| p b.title}. In this context, numbered parameters come really in hand by saving us some keystrokes without losing readability.

Books.all.each { p _1.title }

Pattern Matching

Following the trend of all the major programming languages in adopting functional programming, Ruby is bringing Pattern Matching as an experimental feature. See discussion here

case [10, [20, 30, 40]]
    in [num1,[num2, *num34] ]
        p num1
        p num2
        p num34
(irb):1: warning: Pattern matching is experimental, and the behavior may change in future versions of Ruby!
[30, 40]

It also can traverse a hash object and assign its value if a pattern is matched.

require "json"

json = <<END
  "actor": "Keanu Reeves",
  "age": 55,
  "movies": [{ "title": "The Matrix", "year": 1999 }]

case JSON.parse(json, symbolize_names: true)
in {actor: "Keanu Reeves", movies: [{title: "The Matrix", year: year}]}
  p year
(irb):55: warning: Pattern matching is experimental, and the behavior may change in future versions of Ruby!
=> 1999

Improved REPL (IRB)

Let's be honest. Even though irb is really helpful when we want to refresh some ruby syntax, it is not that fun to code on it. IRB now supports multi-line editing, syntax highlighting, inline editing of methods, auto completion and auto indentation.


Beginless Range

A range without explicitly beginning is experimentally introduced.

arr = ['a','b','c','d','a']
#=> ['a','b','c']


It counts the occurrence of each element

arr = ['a','b','c','d','a']


#=> {"a"=>2, "b"=>1, "c"=>1, "d"=>1}

Argument Forwarding

This feature is quite useful when you want just to pass arguments from one parent function to his child. Ruby 2.7.0 made it easy with the argument forwarding (...)

    def foo(...)

Bear in mind that the parenthesis is mandatory.

Separation of positional and keyword arguments

I believe that this is the biggest change brought by Ruby 2.7.0.
Now keyword argument is not considered a normal argument that is a Hash object passed as the last argument. From now on, a keyword is going to be treated as a separated argument like block is.

If you run any ruby code that used the old of of arguments, it is going to show some warnings. This can be annoying especially when we run Rails because it may flood your console. Thanks to rails team, it is being fixed one by one as you can see here on this issue on github.

How to Install?

You can install the new version by using your ruby package manager.
If you use rbenv you can install it by typing rbenv install 2.7.0

Posted on by:

camfilho profile

Carlos Augusto M. Filho


2B||~2B? Microelectronics enthusiastic. Microverse Full Stack student.


Editor guide