Def Method Blog

The thoughts and ponderings of the team @defmethodinc

Ruby 2.5: Ruby’s Christmas Release is Here!

by Joe Leo III

Yesterday, on Christmas, Ruby creator Yukihiro Matsumoto and his development team released Ruby 2.5, the latest version of Ruby. The release schedule has become a tradition and Rubyists around the world look forward to a brand new version of Ruby to play with on Christmas day. As the Founder of Def Method and co-author of The Well-Grounded Rubyist, I actively track what features are being updated or added and am always excited to “unwrap” the latest Ruby release!

Below are the top three features you’ll want to know about with the release of Ruby 2.5 this Christmas.

1. Bundler, bundled!

Whenever I install a new version of Ruby the very next thing I do is install the bundler gem:

=> gem install bundler

Bundler is key to both installing, organizing, and updating gems. Rubyists use it all the time, but until now the bundler library has been an external gem. This means we had to install a gem to manage our gems!

Beginning with Ruby 2.5, bundler is now a part of Ruby’s core library. You’ll be able to bundle install to your heart’s content as soon as you install Ruby.

This isn’t the first time Ruby has incorporated a gem into its standard library. These days, popular gems like rake, minitest, and did_you_mean are all packaged together in the standard Ruby installation.

2. Object#yield_self

Ruby continues to embrace functional programming principles with this new method for the Object class. yield_self is similar to the way apply is used in other functional programming languages and a close cousin of the tap method. Ben Lewis has a great description of Object#tap, and yield_self has even more to offer. The method receives a block, yields itself, and returns the value of the block:

2.yield_self { |x| x + 5}
=> 7

yield_self promotes method chaining over nested functions. Because each call to yield_self returns the value of the block, it can be called repeatedly:

In addition to method chaining, Ruby gives you the power to write side-effect free code, build recursive functions, and employ lazy evaluation. These are all foundations of functional programming. yield_self can be used to further your exploration of FP, but it can also be useful in many other contexts. We take a functional programming deep dive in the advanced chapters of The Well-Grounded Rubyist.

3. IRB Power-ups

For years now, I’ve been largely ignoring Interactive Ruby (IRB), the built-in REPL for Ruby, in favor of pry. Pry is a gem that can do everything IRB can do, plus a little extra in terms of debugging. (John Backus has much more to say about Pry.) Ruby began adding its own debugging tools to IRB in version 2.4. With the release of version 2.5 Ruby’s debugging capabilities are finally up to snuff.

Since Ruby is a dynamic language we can’t rely on a compiler for debugging. Instead, debugging works by inserting a “binding” into the part of the code base you’d like to analyze and then running your program through that binding. Once the binding is hit, the program pauses and the output is shown to you.

Before Ruby 2.5, the IRB library needed to be required in order to invoke the binding:

Now the require statement is unnecessary. In addition, the debugger gives a few lines of source code before and after your binding so that when the program pauses you have some context to do your debugging. In versions 2.4 and earlier, the only thing to appear on your terminal would be the prompt:

=>

That wasn’t very helpful! Now you’ll have enough source code to get your bearings:

As is typical, the latest Ruby release has something for everyone. Check out the Ruby core team’s latest announcement for even more great features and fixes in Ruby 2.5. What are your top 3 features of Ruby 2.5? Shout them out on Twitter and we’ll discuss! In the meantime, Happy Holidays!