What's New in Ruby 2.5?

27 Dec 2017

The Ruby core team has traditionally released a new version of Ruby
on Christmas day,
and this Christmas,
we got Ruby 2.5.
A couple of years ago,
I started posting a summary of Ruby changes
(2.4,
2.3),
and this year’s version is here.

Rescue in do/end blocks

If you needed to rescue an exception in a block,
the only way to do that was to put it in a begin/end block.
Now, you can rescue exceptions inside all do/end blocks.

(1..5).eachdo|n|do_something(n)rescueSomeError=>eputsenextend

Top level constant lookup is removed

In previous versions,
if Ruby couldn’t find a constant in the current scope,
it would fallback to the top level constant,
and emit a warning.
This has changed in 2.5,
and this will now cause an exception.

Backtrace in reverse order

When printing to console,
Ruby will display the backtrace in reverse order.
You will also see the error message in bold and underlined.
This is an experimental feature,
and the order won’t be changed when printing to logs.

When working on Rails projects,
it is common to have to scroll a long way back
to find the line in the backtrace
that shows the line where the exception happened.
By reversing the backtrace,
you get the context on the line causing the exception
without having to scroll back.

Kernel#yield_self

This method passes an object to a block
and returns the value returned by the block.
The use of pipelines is common in Elixir,
so I’ll translate some
code
from one of my Elixir packages to Ruby
to demonstrate this feature.

The Ruby community has many people
who are interested in Elixir,
so this could end up being a popular feature.
I wonder if this could have been an operator,
but it’s still an interesting addition to Ruby.

Standard Gems

Ruby comes with a lot of things bundled into the standard library,
and this makes it hard to push bug fixes after a Ruby version is released.
To solve this, many libraries are being gemified,
and released independently of Ruby.
Libraries like cmath and webrick have been extracted this way.

This doesn’t mean you can’t use Webrick the next time you install Ruby, though.
The gems being extracted will be shipped with Ruby as default gems,
but you also have the option of installing the latest version through Rubygems.

Array methods

#append and #prepend methods have been added,
which are aliases for #push and #unshift respectively.
These aliases were already introduced in Rails via ActiveSupport,
but it’s nice to see them introduced into Ruby.

Goodbye, ubygems.rb!

This change will have zero impact on how you use Ruby,
but I was curious why we had an ubygems.rb file in stdlib,
so I wanted to mention it now that it’s been removed.

Ruby has a -r flag that lets you require a library,
e.g. ruby -rmath to require 'math'.
The rubygems file was named ubygems.rb
so that the flag could be used as
-rubygems instead of -rrubygems.
This file hasn’t been needed since Ruby 1.9,
and has now been removed from stdlib.

Links

Hi, I’m Nithin Bekal.
I work at Shopify in Ottawa, Canada.
Previously, co-founder of
CrowdStudio.in and
WowMakers.
Ruby is my preferred programming language,
and the topic of most of my articles here,
but I'm also a big fan of Elixir.
Tweet to me at @nithinbekal.