Ruby 3.0 lands. Click your heels together 3 times and say: Speed up programs, speed up programs, speed up programs

Ruby 3.0 lands. Click your heels together 3 times and say: Speed up programs, speed up programs, speed up programs

Five years in the making, performance-oriented Ruby 3.0 has finally landed providing programmers with parallel execution features, and first tools which should lead the language towards a future of static type checking.

DevOps folks will be most familiar with Ruby through its use in automation tools Puppet and Chef, though it is mainly used for building web applications such as Basecamp and Airbnb nowadays. The language follows an object-oriented approach and was developed by Yukihiro Matsumoto, or Matz for short, in 1993. 

Influenced by Perl, Smalltalk, Lisp and others, Matz aimed to come up with a “natural, not simple” programming language that blends functional and imperative programming. The first official version was released in 1995 and Ruby has since made itself a home amongst the top 15 programming languages in various rankings.

Following the goal to make Ruby 3 “3 times faster than Ruby 2”, Matz and his community put a focus on improving the JIT in a way that boosts performance for applications that frequently call the same few methods. To get there, they tackled JIT-ed code and the compilation process itself, avoiding garbage collection or executing it in the background, and reducing the number of locks between Ruby and JIT threads. 

Ruby 3 also comes fitted with deduplication routines for native functions shared by multiple methods, method inlining support for some C methods, and instance variable improvements such as access optimisations in core classes, and a reduction of redundant checks.

Another concept to improve performance and pay respect to the “multi-core age” is concurrency. Deemed “very important” by language creator Matsumoto, Ruby 3 stacks up in this regard and includes a scheduler for Async Fiber as well as an actor-model like concurrent abstraction called Ractor. The latter is still experimental but can already be used to implement thread-safe parallel programs by anyone ready to make some slight adjustments once specification and implementation have matured. 

To further future-proof itself, Ruby 3.0 also includes a new language called RBS and TypeProf, a type analysis tool, which are the “first steps” towards a “future with static type checking, without type declaration, using abstract interpretation” which boils down to less error-prone code. RBS allows users to describe the types of Ruby programs, so that tools can understand them better. TypeProf is one of those tools and currently “reads non-annotated Ruby code, tries inferring its type signature, and prints the analysis result in RBS format.”

The Ruby language itself also saw some changes, since keyword arguments are now separated from positional ones, procs that accept a single rest argument are no longer autosplatted, and pattern matching has left the experimental stage behind. Developers working a lot with one-line pattern matching should check their redesign, and there’s an experimental find pattern available.

To make sure outdated constructs aren’t used, programmers will now have to turn deprecation warnings on themselves. They are no longer shown by default, since this led to some frustration during Gem generation, but this can be fixed by using the -W:deprecated option. Additional details can be found in the NEWS file for version 3.0.