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:

(1..)

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]] [1,2,3,4,5][1..] # [2, 3, 4, 5] (1..).step(5).take(100) # [1, 6, 11, 16, 21, 26, 31, 36, 41, 46]

What other examples can you come up with?

Enumerable::ArithmethicSequence

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.

Example:

(1..10).step(2).first # 1 (1..10).step(2).last # 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.merge(b).merge(c) # {: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.

Example:

Integer("a") # 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.

Example:

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

Random.bytes

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

Here’s an example:

Random.bytes(10) # "\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' SecureRandom.bytes(10)

Why bother with this new method then?

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

Range#%

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

Example:

((0..) % 2).take(5) # [0, 2, 4, 6, 8]

This method is equivalent to Range#step .

TracePoint#parameters

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.

Example:

TracePoint.trace(:call, :b_call, :c_call) do |tp| p [tp.event, tp.parameters] end def orange(a,b,c*) end orange(1,2,3)

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.

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.

Example:

[1,2,3,4,5].difference([3]) # [1, 2, 4, 5] [1,2,3,4,5].union([5,6,7]) # [1, 2, 3, 4, 5, 6, 7]

These new methods take multiple arguments.

Summary

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!