Released quite recently, Ruby 2.6 brings a bunch of conveniences that may be worth taking a glimpse of. What is new? Let’s give it a shot!
1. Endless ranges
Ruby 2.6 introduces endless ranges, which makes expressions like:
Ruby 2.6 provides new methods and aliases for Array class. I.e:
- Array#difference – works like “I”‚ operator, but accepts multiple arguments.
- Array#union – works like “&” operator, but accepts multiple arguments.
- Array#filter – new alias for Array#select.
Hash#merge and Hash#merge! accept now multiple arguments. No more code like: hash1.merge(hash2).merge(hash3)!
Ruby 2.6 provides method Kernel#then, which is an alias to Kernel#yield_self, allowing us to chain operations into pipelines and making the code more readable.
5. New way to create a hash out of the array
Ruby 2.6 provides Enumerable#to_h method which accepts a block and maps keys to values!
Let’s remind shortly how we had to deal with creating a hash out of array before Ruby 2.6:
From now on, it’s possible to do this simply like this:
How cool is that!
6. Proc#compose – a little bit of Ruby magic or just an unreadable mess?
Since Ruby 2.6 Proc#>> and Proc#<< methods were added, allowing us to combine two functions. Let’s look at this:
With the #>> operation, the number gets into exponentiation function first and then minus_two is performed on the result of the first function call. Although it seems useful and gives us a bit of magic, that we all love Ruby for, some say that code written like this becomes just an unreadable mess.
These are, in my opinion, the most interesting conveniences which Ruby 2.6 provides us with. I highly recommend taking a glimpse of official changelog too.