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:
possible.
2. Array
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.
3. Hash
Hash#merge e Hash#merge! accept now multiple arguments. No more codice like: hash1.merge(hash2).merge(hash3)!
4. Kernel#then
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#>> e 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.
Leggi anche:
– Writing documentation has become easy thanks to VuePress
– Security in Javascript packages
– Vue.js basics tutorial. How to start with this framework?