At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

,

Ruby 2.6.0 Released! Here's The Overview Of All New Features In Ruby 2.6.0

  • By Venkata Krishna
  • February 25, 2019
  • 2098 Views

Recent Ruby 2.6.0 released really got some cool features which attracted me to explore more on it. On search of witnessing some major features i found handful of new updates which are really useful & optimistic. So, am planning to share all my views on its major features in this article. These updates includes Infinite Ranges, Ruby 2.6 benchmark, Ruby 2.6 Infinite Hashes, Ruby 2.6 union, Ruby 2.6 filter, Ruby 2.6 Enumerable chain and so much more. Let’s have a quick tour into ruby 2.6.0

Installing The Ruby 2.6.0 version:

 

  1. Click the link, it will download the zip file for you then you can extract that file.
  2. If you are using rvm then use this command to install “rvm install ruby-2.6.0”

Alright! You must be done with the installations. Now, let’s look into the futures.

New Features In Ruby 2.6.0 version

 

1) Infinite Ranges

In Ruby 2.6.0 we can use infinite ranges or endless ranges. Before 2.6 we can achieve endless ranges with Float::INFINITY, In Ruby 2.6.0 we can get endless ranges directly as i shown below.

(1..Float::INFINITY).step(2).take(10).map(&:to_i)
=> [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

 
 

In Ruby 2.6.0

(1..).step(2).take(10)
=> [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]

 

2) Merging Hash By Passing Multiple Hashes As Arguments

In previous versions, if we want to merge multiple hashes with one then we need to use merge 2 times as shown in below example.
Sample of merging hash2 and hash3 with hash1,

hash1  = { one: 1 }
hash2  = { two: 2 }
hash3 =  { three: 3 }
 
hash1.merge(hash2).merge(hash3)
=> {:one=>1, :two=>2, :three=>3}

 

In Ruby 2.6.0

We can pass two hashes as arguments as shown below,

hash1.merge(hash2, hash3)
=> {:one=>1, :two=>2, :three=>3}

 

3) Bytes Method In Random Class

Ruby 2.6.0 introduces bytes method in Random class to get some random bytes.
We can also achieve this in older versions with the help of SecureRandom class bytes method.

require 'securerandom'
SecureRandom.bytes(20)
=> "\n\x9D\r0\xA3\t\xF3m\xA5\xD19Nf~\xD4\xFC\xEC\aS\xB3"

 

In Ruby 2.6.0

You can use Random.bytes method which is 8 times faster than the SecureRandom.

Random.bytes(20)
=>  "\x16\".\xB9Mhe\x93\xC8\x90\x95\x92v\xBDE\n\x11\xF5\xD7:"

4) % in Ranges

This version introduces % in Ranges, this is an alias of step and here is an example to use this in ruby 2.6.0

((0..100) % 5).take(5)
=> [0, 5, 10, 15, 20]

Same thing you can achieve it using step as follows,

(0..100).step(5).take(5)
=> [0, 5, 10, 15, 20]

 
5) Union And Difference Methods In Array
This version introduces 2 new Array methods called union and difference which requires more efforts to get it done in older versions.
Now can easily perform array operations with this,

[1,3,6,8,10].difference([1,6])
=> [3, 8, 10]
 
[1,3,6,8,10].union([1,6,2,9])
=> [1, 3, 6, 8, 10, 2, 9]

 
6) to_h accepts block
If we want to do some operations on Hash then we have to use map and to_h in old version. Ruby 2.6 accepts block to the to_h method.

Old Version

a  = { b: 2, c: 3 }
a.map { |k,v| [k,  v+1] }.to_h
=> {:b=>3, :c=>4}

 

In Ruby 2.6

a.to_h { |k,v| [k, v+1]}
=> {:b=>3, :c=>4}

 
Similarly, we can use this in Ranges and Array too

(1..10).to_h { |k| [k, k*2] }
=> {1=>2, 2=>4, 3=>6, 4=>8, 5=>10, 6=>12, 7=>14, 8=>16, 9=>18, 10=>20}

 
7) Filter Method In Array
Ruby 2.6.0 introduces filter method in Array that works same like select and also it’s an alias of select.

[1,2,3,4].select {|a| a == 2}
=> [2]

 

In ruby 2.6.0

2.6.0 :105 > [1,2,3,4].filter {|a| a == 2}
=> [2]

 
 

8) Enumerable chain and + methods

This returns Enumerable chain object.

Old Version

list = (1..3).each + (5..7) + (9..10)
=> #<Enumerator::Chain: [#<Enumerator::Chain: [#<Enumerator: 1..3:each>, 5..7]>, 9..10]>
2.6.0 :016 > list.to_a
=> [1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12]

 

In Ruby 2.6.0

(1..4).chain((6..9), [10, 12]).to_a
=> [1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12]

 

9) Handling Exceptions

Normally in ruby, it’s not possible to convert the string into integer and even If we try to convert it will get an exception.

Integer("example")
ArgumentError (invalid value for Integer(): "example")

 
Ruby 2.6.0 introduces adding exception option as a argument,

Integer("example", exception: false)
=> nil

 
Similarly, we can also add the exception argument to Integer(), Float(), Rational(), Complex() too.

10) ArithmeticSequence ‘first’ and ‘last’ methods

In Ruby 2.6.0, we can get the first and last elements in an Arithmetic sequence.

(1..10).step(2).first
=> 1
2.6.0 :023 > (1..10).step(2).last
=> 9

 
In 2.6.0, we can also compare the 2 Arithmetic sequences, for example

(1..10).step(2) == (1..10).step(2)
=> true

 
That’s all guys! Let us know your favorite features in comment section.
Want to learn more? Trying to be a smart coder? Curious to learn more tactics from the group of Ruby & full stack experts! Subscribe us below to post you our top engaging blogs & tips.
[contact-form-7 404 "Not Found"]