L O A D I N G
blog banner

Ruby 3 is released: The list of Ruby 3 features

Ruby 3 major updates

1. Ruby 3 performance:
Ruby 3, 3 times faster than Ruby 2.

CPU optimization
MJIT implementation is different from the usual JIT. When methods get called repeatedly e.g. 10000 times, MJIT will pick such methods which can be compiled into native code and put them into a queue. Later MJIT will fetch the queue and convert them to native code.

Memory optimization
Ruby 3 comes with an enhanced garbage collector. It has python’s buffer-like API which helps in better memory utilization.

2. Parallelism and Concurrency in Ruby 3
Concurrency is one of the important aspects of any programming language.

Fibers:
Fibers are considered a disruptive addition in Ruby 3.
Fibers are light-weight workers which appear like Threads but have some advantages. It consumes less memory than Threads. It gives greater control to the programmer to define code segments that can be paused or resumed resulting in better I/O handling.

Ractors:
Ractors allows Threads in different Ractors to compute at the same time.
Ractor is an Actor-Model like concurrent abstraction designed to provide a parallel execution without thread-safety concerns.
Each Ractor has at least one thread, which may contain multiple fibers. In a Ractor, only a single thread is allowed to execute at a given time.

def tarai(x, y, z) =
  x <= y ? y : tarai(tarai(x-1, y, z),
                     tarai(y-1, z, x),
                     tarai(z-1, x, y))
require 'benchmark'
Benchmark.bm do |x|
  # sequential version
  x.report('seq'){ 4.times{ tarai(14, 7, 0) } }

  # parallel version
  x.report('par'){
    4.times.map do
      Ractor.new { tarai(14, 7, 0) }
    end.each(&:take)
  }
end

Benchmark result:
          user     system      total        real
seq  64.560736   0.001101  64.561837 ( 64.562194)
par  66.422010   0.015999  66.438009 ( 16.685797)

source: https://www.ruby-lang.org/en/news/2020/12/25/ruby-3-0-0-released/


3. Static Analysis:
RBS:
RBS is a language to describe the structure of a Ruby program.
You can write down the definition of classes and modules: methods defined in the class, instance variables and their types, and inheritance/mix-in relations.
The .rbs files are something similar to .d.ts files in TypeScript. 

Example:

# user.rbs

class User
  attr_reader name: String
  attr_reader email: String
  attr_reader age: Integer
  attr_reader posts: Array[Post]

  def initialize: (name: String,
                   email: String,
                   age: Integer) -> void

  def each_post: () { (Post) -> void } -> void
                   | () -> Enumerator[Post, void]
end

TypeProf:
TypeProf is a type analysis tool bundled in the Ruby package.
Currently, TypeProf serves as a kind of type inference.
It reads plain (non-type-annotated) Ruby code, analyzes what methods are defined and how they are used, and generates a prototype of type signature in RBS format.

An example input:

# test.rb
class User
  def initialize(name:, age:)
    @name, @age = name, age
  end
  attr_reader :name, :age
end
User.new(name: "John", age: 20)

An example output:

$ typeprof test.rb
# Classes
class User
  attr_reader name : String
  attr_reader age : Integer
  def initialize : (name: String, age: Integer) -> [String, Integer]
end

You can run TypeProf by saving the input as “test.rb” and invoking the command “typeprof test.rb”.

4. Other Ruby 3 features and changes
One-line pattern matching is redesigned.
1. => is added. It can be used like a rightward assignment.

2. in is changed to return true or false.

Find pattern is added.
Endless method definition is added.
Hash#except is now built-in.
Memory view is added as an experimental feature

Leave a Reply

Your email address will not be published. Required fields are marked *