Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Ruby 2.5: Ruby’s Christmas Release Is Here!

DZone's Guide to

Ruby 2.5: Ruby’s Christmas Release Is Here!

Ruby is one of the most popular languages among web devs, and receives annual updates every December. This year's changes are in! Find out what's new in Ruby!

· Web Dev Zone ·
Free Resource

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

This 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 when you unwrap 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:

2.yield_self { |x| x + 5 }.

yield_self { |x| x * 2 }.

yield_self { |x| x - 1 }

=> 13

Or, it can be easily placed within an existing function:

name = “Joe Leo”

tokens = name.split(“ “)

attrs = { first: tokens[0], last: name.yield_self { |n| tokens.size == 2 ? tokens[1] : tokens[2] } }

=> {:first=>”Joe”, :last=>”Leo”}

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:

require ‘irb’ #no longer needed in Ruby 2.5!

def i_need_a_debugger

x = 5

binding.irb

x -= 3

x

end

i_need_a_debugger

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:

From: c.rb @ line 3 :

      1: def i_need_a_debugger
      2: x = 5
=> 3: binding.irb
      4: x -= 3
      5: x
      6: end
      7: i_need_a_debugger

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!

Deploying code to production can be filled with uncertainty. Reduce the risks, and deploy earlier and more often. Download this free guide to learn more. Brought to you in partnership with Rollbar.

Topics:
ruby ,gems ,bundling ,ruby on rails ,web dev

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}