Ruby 2.3.0 – Review

Good to see you again…
We have seen the features implemented in Rails 5, in the earlier blog.
Now its time to go with Ruby 2.3.0
This is the first stable release of Ruby 2.3 series and it has come up with many new features.
We are going to see some of them in this post.
# Install using RVM
rvm install 2.3.0
# Install using Rbenv
rbenv install 2.3.0

Frozen String Literal Pragma:

With Ruby 2.1, “str”.freeze has been optimized to reduce object allocation. Ruby 2.3 introduces a new magic comment and command line option to freeze all string literals in the source files.

Using frozen (immutable) strings gives us improved performance because Ruby now has to allocate fewer objects.

To make the transition easier, Ruby 2.3 allows you to optionally make all strings literals frozen by default.

Ruby-1

Fresh arrivals:

  • new pragma, frozen-string-literal has been experimentally introduced.
  • –enable/–disable=frozen-string-literal options also have been introduced.
  • command line options –debug or –debug=frozen-string-literal enable additional debugging mode which shows created location with at frozen object error (RuntimeError).

Safe Navigation Operator:

&.”, which already exists in C#, Groovy, and Swift, is introduced to ease nil handling as “obj&.foo”.

It can be very useful in cases where you need to check if an object is nil before calling a method on it. It will return nil if the object equals nil, otherwise it calls the method on the object.

Ruby-2

did_you_mean:

When a NameError or NoMethodError occurs because of a typo in the name, the did_you_mean gem automatically suggests other names similar to the method name.

Ruby-3

Making error messages more helpful has a huge impact on making the language easier to use, especially for beginners.

“Array#dig” and “Hash#dig”:

The new #dig instance methods provide concise syntax for accessing deeply nested data.

We can now access nested elements in arrays and hashes with a much simpler API.

Dig in action with Array

Ruby-4

Dig in action with Hash

Ruby-5

Hash Comparison:

Hashes now have the comparison methods defined on them.

It provides “Hash#<=“, “Hash#<“, “Hash#>=“, and “Hash#>” operators to perform superset and subset operations.

Ruby-6

Hash#to_proc:

“Hash#to_proc” returns a lambda that maps the key with the value. When you call the lambda with a key, it returns the corresponding value from the hash.

Ruby-7

Enumerable#grep_v:

This method is inverse of the method “Enumerable#grep”.

The grep_v method is equivalent to the -v option in the command line grep utility. It returns the list of items that do not match the condition.

Ruby-8

 

Numeric#positive? and Numeric#negative?:

These functions have been around in Rails core extensions for a while, and now have been included in Ruby.

Numeric values now have predicate methods that check if the subject is positive or negative.

Ruby-9

Until next time…..

Surender,
ROR Developer,
Mallow Technologies.

Leave a Reply

%d bloggers like this: