Static Typing in Ruby 3.0
Learn about what’s coming next in Ruby
This week, Ruby’s development team released the first preview build of their long-awaited major update to version 3.0!
Among the countless cool changes and additions — including some fantastic general performance boosts and a powerful new concurrency/parallel processing efficiency model — a new built-in language has emerged for static type definitions: Ruby (Type) Signature language, or RBS.
Ruby is a dynamically typed language, which means the interpreter tries to infer the data type of variables and object properties at runtime. This generally leads to programs being more dynamic and easier (faster) to code, and the interpreter/compiler loading code faster. However, the need for type inference and its associated costs, as well as the difficulty in detecting errors at compile time with dynamically typed code, have caused a modern programming trend to move towards statically typed languages.
Static typing makes the development experience much smoother. Bugs are far easier to detect, especially with modern IDEs and their linting as you write. For most statically typed languages, their more explicit nature leads to faster-compiled code (more efficient resulting machine code.)
Types in Ruby
With the upcoming version 3.0, Ruby has plans to integrate optional static typing as well!
The devs have created a “foundational type signature language” gem in the new version preview called RBS. The new
.rbs files are analogous to TypeScript’s
.d.ts type definition files. These files contain all your various modules’ type definitions. By keeping them in separate files, your actual Ruby code need not be changed and you can quickly and easily add the benefits of static types to your Ruby programs!
In addition to traditional type definition syntax that looks pretty much as you would expect a Ruby version to look, RBS also implements interfaces for safer “guided” Duck Typing, “union types,” and “method overloading” to account for non-uniform Ruby types, as well as a few other cool features.
RBS is designed to be a standardized foundation for static typing in Ruby. The current leader in typing solutions in Ruby is Sorbet, which is still being supported in the new versions of Ruby. RBS is being developed in tandem with Sorbet, with the latter being the higher-level type checker that will implement the RBS files.
With the industry-wide trend towards adopting static typing in large scalable applications, the addition of RBS in Ruby 3.0 is an exciting step forward. Taking the development experience efficiency improvements that typing brings and combining them with the big runtime performance boosts under the hood in Ruby 3.0, I think the future is looking really bright for Ruby in the software industry.