9 New Features in Ruby 2.6

A new version of Ruby is coming with new features & performance improvements.

Would you like to keep up with the changes?

Let’s have a look!

Endless Ranges

Ruby 2.5 & older versions already support one form of endless range (with Float::INFINITY), but Ruby 2.6 takes this to the next level.

The new endless range looks like this:


This is different from a regular range because it doesn’t have an ending value like (1..10).

Example uses:

["a", "b", "c"].zip(1..)
# [["a", 1], ["b", 2], ["c", 3]]

# [2, 3, 4, 5]

# [1, 6, 11, 16, 21, 26, 31, 36, 41, 46]

What other examples can you come up with?


A new kind of Enumerable object has been introduced in Ruby 2.6.

I’m talking about Enumerable::ArithmethicSequence.

Right now there are two methods that give you an ArithmethicSequence:

  • Range#step
  • Numeric#step

What’s special about this?

An ArithmethicSequence knows how many elements there are, what is the first element & the last element.


# 1

# 9

These first & last methods aren’t available before Ruby 2.6 & that’s why ArithmethicSequence exists now!

Another difference:

(1..10).step(2) == (1..10).step(2)
# false - Ruby 2.5 (and older)

(1..10).step(2) == (1..10).step(2)
# true - Ruby 2.6

Arithmetic sequences can be compared to each other in 2.6.

Merge Hash With Multiple Arguments

If you want to merge multiple hashes so you can combine them into one…

You can do this:

a = { a: 1 }
b = { b: 2 }
c = { c: 3 }

# {:a=>1, :b=>2, :c=>3}

Ruby 2.6 adds a new way:

a.merge(b, c)
# {:a=>1, :b=>2, :c=>3}

Same results, but you only have to call the method once!

New Exception Option

When you use a conversion method like Integer you’ll get an exception if the value can’t be converted.


# ArgumentError (invalid value for Integer(): "a")

Ruby 2.6 adds a new exception keyword argument to:

  • Integer()
  • Float()
  • Rational()
  • Complex()

You can control the behavior of these methods with this keyword.


Integer("a", exception: false)
# nil


If you need some random bytes Ruby 2.6 adds a new bytes method to the Random class.

Here’s an example:

# "\xCD\r\xE6Wz\xBA)\x02\xC4\xDB"

This isn’t completely new functionality as this has always been available through the securerandom module.

require 'securerandom'


Why bother with this new method then?

Random.bytes trades security for speed, it’s 8x faster than SecureRandom.


A new % method has been added to Range in Ruby 2.6.


((0..) % 2).take(5)

# [0, 2, 4, 6, 8]

This method is equivalent to Range#step.


The TracePoint class helps you trace events like method calls, class definitions & threads.

Ruby 2.6 adds a new parameters method.

With this new method you can print the parameter list for the method being called.


TracePoint.trace(:call, :b_call, :c_call) do |tp|
  p [tp.event, tp.parameters]

def orange(a,b,c*)


Results in:

[:call, [[:req, :a], [:req, :b], [:rest, :c]]]

Transient Heap

The transient heap is a performance improvement for short-lived objects that aims to reduce the problem of memory fragmentation & slow calls to malloc.

Malloc is how the Ruby interpreter (and most C programs) request memory from the operating system.

According to the NEWS entry, we are looking at a 6-7% speedup.

I ran some benchmarks:

It looks like this is most effective when creating many hashes smaller than 10 elements.

Ruby 2.6 Transient Heap

Bigger hashes are slower in Ruby 2.6 (preview 3) based on these results.

Other objects that can benefit from Transient Heap:

  • Arrays
  • Struct
  • Regular objects (from classes you create)

Array#union & Array#difference

Two new methods, union & difference are added to the Array class in Ruby 2.6.


# [1, 2, 4, 5]

# [1, 2, 3, 4, 5, 6, 7]

These new methods take multiple arguments.


Ruby 2.6 is coming with new useful features to help you write better code in less time. Another new performance-related feature, MJIT (Method Based Just-in-TIme Compiler) will be covered in a dedicated article.

Ruby 2.6 is expected to be released in December 25, 2018.

If you want to keep up with the changes, improve your Ruby skills & become an awesome Ruby developer don’t forget to join my Ruby newsletter today.

Thanks for reading!

12 thoughts on “9 New Features in Ruby 2.6”

  1. Merry Christmas to us! I haven’t had a chance to stay up to day on the upcoming Ruby releases, so this was a nice quick read to help catch up. The new TracePoint params update looks to be interesting since I have been trying to work on my own open source performance tool. Will have to see where that fits in. Thanks for the post!

Comments are closed.