What’s new in Ruby 2.5


Bookmark and Share

The Ruby language has increasingly stabilized over the years. The upcoming release of Ruby 2.5 is not going to let us down too. It introduces lots of new features and improvements over the previous version. The first preview was released on 10th October 2017 and the final build will be released on this 25th. This blog dissects into this latest and exciting release and goes through some of the most important changes, we will be writing another article on performance improvement once the 2.5 if officially released.

  • Added Hash#transform_keys method

Ruby 2.4 added the transform_values method, 2.5 completes it by adding transform_keys thus make it a perfect pair.

Hash#transform_keys can change keys according to the return value of a block:

transform_keys! is a destructive version:

We know how many times we had to manipulate the keys of a hash. transform_keys is gonna be a game changer and going to be very compelling to be used in your legacy app. The destructive version is a just silver lining to this.

  • Array#prepend and Array#append

Ruby 2.5 brings home two new aliases which are so much better than the two of the most used operations in the language. Array#prepend and Array#append are more programmer friendly than the …

Read More

Crystal tuples: the immutable data structure of crystal


Bookmark and Share

Tuple is a data structure which has immutable elements and is of a fixed size. Its similar to an array, but unlike arrays in crystal and ruby which allows adding more values over time a tuple is of fixed and cannot change. (Disclaimer: This article is meant for Ruby developer and explaining what a tuple is to a ruby developer).

In crystal we have two types of tuple

1) Tuple {1, “hello”, :world}
2) NamedTuple {x: 1, y:2, z: 5}

They are immutable, which means if you try changing the value of an element in a tuple you will get an exception. Since crystal programs are compiled before execution you will get to see these errors while you compile the program itself.

example:

If you try assigning a value to it, like so

You will get an exception like bellow.

In crsytal, tuples are the preferred way to return a multiple results from a method.

For example inside the crystal core we have a method to get the minimum and maximum of an array.

the result would be {1, 100}

Note: Since we just mentioned minmax, have a look at minmax_by method as well. It would let you apply a block of code over your range and then …

Read More