Jan 7, 2021
Ruby 3.0 - Faster Than Before

The basic principle of ruby 3 is to keep compatibility and make the language faster and more productive. Thus, the main three aspects of the new version are making ruby fast, concurrent, and correct.

Faster Ruby:

The speed is achieved by improving the performance of MJIT. As a result, ruby runs three times faster than previous versions in some benchmarks, though not with the rails applications.

Concurrent Ruby:

In this multi-core age, concurrency is gained with Async I/O Fiber and Ractor.

  • Async I/O Fiber: A fiber for heavy I/O tasks. This doesn’t utilize the multi-core but utilizes blocking time to improve the performance of I/O tasks. This fiber is based on the technology used by the Ruby application server – Falcon.
  • Ractor: It stands for Ruby Actor which helps in improving CPU intensive tasks. There are isolated object spaces for each ractor and they communicate via channels that enable parallel execution without safety concerns. You can make multiple ractors and you can run them in parallel. Ractor enables you to make thread-safe parallel programs because ractors can not share normal objects. Communication between ractors is supported by exchanging messages. 

Correct Ruby:

Ruby seeks the future with static type checking, without type declaration, using abstract interpretation. RBS & Type Profiler are the first step in the future.

  • RBS (Ruby Signature):  It is a language similar to Ruby, but specially designed to describe types. Ruby 3 ships with RBS for the core library (RBS string, RBS hash, and so on). We can use this type of information in RBS files for type checking and better IDE with better code compilation.


                class Test
 def test: () ->void   #test() returns nothing
 def to_s:() -> String
         |(Integer) -> String   #to_s() takes null or integer as an argument and returns string
  • Type Profiler: It does naive type checks, also generates RBS for your application using abstract interpretation. If you have some type of mismatch in your application, the type profiler will warn you about these errors.


                class Test
  def test(a)
    b = a+2
    return b

How does it work?

  1. The first type profiler will check the new instance of Test which calls test().
  2. Now, test() is bound to value 15 which is an integer.
  3. So, it can be said that a is an integer, meaning the test method takes integer input.
  4. Now, a has an addition (+) operation with 2 (again integer) which will return an integer. So, b is an integer.
  5. Lastly, it can be concluded that test() returns an integer.

RBS file for Test:

                class Test
  def test: (Integer) -> Integer   #test passes int as an argument and returns int.

What’s new:

  • ‘ => ‘  symbol is added which can be used as rightward assignment operator(right assignment)
                >>  1 => a
>> puts a    #prints 1
  • Now ‘in’ returns true or false
                >> O in 1   #returns false in the latest version, earlier it was giving an error.
  • The inline method definition is added.

                >> def square (n) = n*n
  • except() function is added for hash

                >> h = {a:1, b:2, c:3}
>> puts h.except(:a)  #prints {:b => 2, :c => 3}

Sachiin Gevariya

Sachin Gevariya is a Founder and Technical Director at Essence Solusoft. He is dedicated to making the best use of modern technologies to craft end-to-end solutions. He also has a vast knowledge of Cloud management. He loves to do coding so still doing the coding. Also, help employees for quality based solutions to clients. Always eager to learn new technology and implement for best solutions.