Static Typing in Ruby 3.0

Learn about what’s coming next in Ruby

Frank Gerold
Oct 1 · 3 min read
A red stone set to a pitch-black background.
A red stone set to a pitch-black background.
Photo by Joshua Fuller on Unsplash.

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.

Static Typing

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.)

Popular dynamically typed languages have begun to follow suit. Python’s typing library for optional static type definitions has gained significant traction since its introduction in Python 3.5. Microsoft’s statically typed JavaScript superset TypeScript has seen a meteoric rise in popularity in recent years and is basically an industry standard at this point.

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.

Sorbet

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.

Conclusion

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.

Better Programming

Advice for programmers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store