What’s New In Ruby 2.6.0 Preview 1 ?

 

Hi Folks, Recently Ruby 2.6.0 Preview-1 Released, This updates includes various features to improve the overall performance of Ruby.

 

Let’s explore this features and check out how this will help us.

 

To Explore this features, we must install Ruby 2.6.0-Preview1 version.

 

Let’s look into the installation procedure,

Using Ruby 2.6.0 preview-1 version

 

1) If you are using rvm, you can install Ruby 2.6.0-preview1 version by using the command

 

rvm install ruby 2.6.0-preview1

 

Or you can directly download from here

 

1.ruby 2.6.0 preview1

 

 

2) Now set rvm to use ruby 2.6.0-preview1

 

You can set this by the following command.

 

rvm use ruby 2.6.0-preview1

 

2.using ruby 2.6.0 preview1

 

 

With the ruby -v command you can find which version of Ruby you are using.”

 

In our case, this command returns back this result “ruby 2.6.0preview1 (2018-02-24 trunk 62554) [x86_64-linux]”

 

Hope I had cleared the installation process now it’s time to explore the features of Ruby 2.6.0.

Features of Ruby 2.6

 JIT Compiler in Ruby

 

Ruby 2.6 introduces an initial implementation of JIT(Just-in-time) compiler.

The intention behind the JIT compiler is to “Improve the performance of a Ruby Program Execution.”

How to use JIT compiler

 

i) Just type –jit in a command line or you also have an alternative way to achieve it with $RUBYOPT environment variable.

 

Example

 

Consider this simple ruby program test_ruby_jit.rb which will print the welcome message as “Welcome to Ruby JIT Compiler.”

 

You can note the difference between normal ruby execution and ruby with JIT complier.

 

Normal Ruby Program Execution

 

3.sample_program

Ruby with JIT compiler

 

4.using jit

 

ii) by specifying –jit-verbose=1 you can get the basic information of ongoing JIT compilation.

 

5.jit verbose

 

The main purpose of this JIT release is to,

 

  • Find out whether it works for your platform
  • To find out security risks before the 2.6 release

 

Currently, JIT compiler is supported only when Ruby is built by GCC or clang, and the compiler is available at run time.

 

This release is NOT ready for benchmarking final performance of Ruby’s JIT compiler, especially for large platforms like Rails Applications.

 

Other New Features

 

Other new methods are also added in the Ruby 2.6 version.

 

To check out those methods, you can use “Interactive ruby” by typing the command “irb”.

 

Binding Class

 

=> Added new instance method “source_location” for Binding class

 

=> You can check this method inside Binding.instance_methods or you can see the method by using this query

 

Binding.instance_methods.map(&:to_s).select {|a| a.include?(“source_location”) }

 

=> This method returns source location of binding two array element ‘__FILE__’ and ‘__LINE__’

 

=> We can get the same information by using eval(“[__FILE__, __LINE__]”,binding) also.

 

Below samples are used to get the same information by handling different methods.

 

Example 1

 

Using source_location method to get FILENAME and LINE number

 

6.source_location_method

 

Example 2

Using eval method to get FILENAME and LINE number

 

7.eval

Random Class

 

=> Added new method “bytes” for Binding class.

 

=> You can check this method inside Random.methods or you can see the method by using query Random.methods.map(&:to_s).select {|a| a.include?(“bytes”) }

 

=> It is equivalent to existing method Random::Default.bytes(number)

 

Example:1 – Random.bytes(2)

Example:2 – Random::DEFAULT.bytes(2)

 

=> Above Two examples will work in the same way.

Dir Class

     

Objects of a class Dir are directory streams, representing directories in the underlying file system.

 

=> Added two new instance methods each_child and children for Dir class.

 

Children

 

This instance method will return an array containing all of the filenames in a given directory.

 

=> You can check this methods inside Dir.instance_methods by using the below command.

 

Dir.instance_methods.map(&:to_s).select {|a| a.include?(“children”) }

 

Example

 

 

8.dir

Each_Child

 

This instance method will call the block once to get the each entry in a named directory, Once the block got executed then the filename will get passed for each entry of a parameter.

 

=> You can check this methods inside Dir.instance_methods by using the below command.

 

Dir.instance_methods.map(&:to_s).select {|a| a.include?(“each_child”) }

 

Example

 

9.dir(1)

 

System Method

 

This method executes the given command in a Subshell. You have to provide the command as a string argument to this method.

 

It returns true on success and false on failure.

 

=> Kernal system method accepts :exception parameter to raise an exception on failure

 

=> before 2.6.0-preview1 version, system will return false on failure, now we can add exception option on failure.

 

So then and there you can clear out the errors.

 

Example

 

10.kernal_system

Features in Exception Handling

KeyError

 

KeyError will railse due to the improper use of Hashes. A Ruby Hash is a series of Key/Value pairs.

 

KeyError#initialize accepts :receiver and :key options to set receiver and key in Ruby code.

NameError

 

NameError will raise when trying to call a undefined variable or symbol name which hasn’t previously declared.

 

NameError#initialize accepts :receiver option to set receiver in Ruby code.

NoMethodError 

 

As clearly indicated by the name, the NoMethodError is raised when a call is made to an object using a method name that doesn’t exist.

 

NoMethodError#initialize accepts :receiver option to set receiver in Ruby code.

Matrix

 

=> added a new method called ‘antisymmetric.’

 

If given matrix is antisymmetric, then it will return “True.” Vice versa If the given matrix is not antisymmetric then it will return “False.”

 

Example

11.matrix_anti_symmetric

Other notable changes

 

=> Speedup `block.call` Where `block` meant for passed block parameter.

 

Ruby 2.5 improves block passing performance. And Additionally, Ruby 2.6 aims to improve the performance of passed block calling.

 

Since it’s a Beta Version i had tried to explore the fewer features. Henceforth will dig more into this features once we get the fuller version.