Statically-typed Ruby: Introducing Sorbet

Statically-typed Ruby: Introducing Sorbet

Sorbet, from Stripe — a fast, powerful type checker, designed for Ruby

Ruby is an amazing language:

A dynamic, open-source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.

Ruby is fairly easy for beginners to adopt. As some of my colleagues say, Ruby holds a new developer’s hand and points them in the right direction. As a dynamic language, that means you wouldn’t know if something is wrong with your code until you run the code. For example, if you call an undefined instance method on an instance of a class you won’t get any error in your code until you run it. But this can be overlooked when you work on a small code base — this is where statically typed languages like Java and C# shine over Ruby.

As software engineers the more code we write the more bugs we introduce into our codebase. As someone once said, the best code is writing no code at all. Static typing lets us define what we expect from our code in terms of data types and return values. This gives us confidence, makes collaboration easy, and means we can get feedback when something goes wrong, even before we run our code. How cool is that? It saves us a lot of time debugging avoidable errors and we can ship quality code faster.

Introducing Sorbet

Sorbet is a fast, powerful type checker designed for Ruby. Built with 💜 at Stripe .

Sorbet is fast and scalable

Sorbet is multithreaded, scaling linearly across cores on your CPU. It checks your types in seconds, giving you feedback as you code.

IDE-ready

Sorbet works with your favorite editor to provide IDE features like autocomplete and jump to definition. It’s easy to add to your CI setup. So, how do we get started using Sorbet? You can start with your existing codebase: Sorbet works with normal Ruby and is compatible with other Ruby tools or Gems you’re already using. Or you can use it for a new project.

Installation

# -- Gemfile --
gem 'sorbet', :group => :development
gem 'sorbet-runtime'
# Install the gems 
❯ bundle install
# Initialize Sorbet 
❯ srb init
# Type check the project 
❯ srb tc

Add the Sorbet Gem to the development group. Run bundle install, rub srb init to initialize Sorbet and Type. Check the project and finally run srb tc.

1_Qbh-MlHIsDkr6Qkvjmy94g.gif

Examples: Sorbet online via the Playground

1_t9zutTiR3pZ2RuSCr4iV6w.png

In the above example, we have a simple Calculate class that has an instance method of addition. To use Sorbet in our class we need to add # typed: true at the top of the Ruby file. Then, after the class definition, we add extend T::Sig. Because Sorbet is non-intrusive, you can define the class and methods that are typed. At the top of the addition instance method, we defined sig {params(x: Integer).returns(String)}. This informed Sorbet that our method required one argument, x, of an integer data type and the method need to return a String datatype. Then we have the main method, outside our class, that initialized two instances of the Calculate class.

We get immediate feedback on what we’re doing wrong in our code before we even run it. How cool is that!

When you hover over the highlighted errors you get more information about the error and what needs to be done to fix it. This can go a long way to avoiding mistakenly passing a wrong argument or datatype to a method — you always know exactly what kind of return value to expect from your method. That’s what I called super-cool!

Let’s fix those errors!

1_CddJrGJ46Hcm-iwsbExQmA.png

Success!

Sorbet’s really a great tool that gives a good developer experience. I hope you find it helpful in your next project.

Official Website Documentation Playground

Thanks for reading!