00:00:00.160
Ali is here to talk about type checking something. Developers are already somewhat on board with that, which is nice. However, for Ruby developers, this is a new and amazing product. Ali is a software engineer from Malaysia, and he's been working on an exciting tool called Sorbet. He writes Ruby and JavaScript at Suria Labs, and aside from these technologies, he is getting his hands dirty with Docker and Kubernetes. He loves Test-Driven Development and service objects. In his free time, he often has his head buried in a book.
00:00:20.080
Welcome to the #NoRuKo conference, a virtual unconference organized by Stichting Ruby NL. You can check out the #NoRuKo playlist with all talks and panels on YouTube. Recorded on the 21st of August, 2020, the NoRuKo website can be found at noruko.org, and more information on Stichting Ruby NL is available at rubynl.org.
00:01:01.680
Today, I'm going to be talking about type checking with Sorbet. First of all, I am Ali Ilman, writing from Malaysia. I work at a company specializing in web and application development, and you can find my email and social media handles online.
00:01:08.800
The content of this talk revolves around Sorbet. It caught my attention because there is an interesting story behind it. Recently, Ruby 3 introduced an official type signature language called RBS. This is something that Max mentioned during his keynote, so we will get into that briefly. It is essential to consider how to add Sorbet to a running production application and the kind of demos we can look forward to.
00:02:06.640
So, what is Sorbet? It is a gradual type checker, which means you don't need to add type annotations throughout your entire codebase. It doesn't matter if you're working with a small codebase or a large one; you can add types incrementally, file by file. Sorbet allows you to write type checks in Ruby syntax, which is not a strict superset of or similar to TypeScript. It is constructed by engineers at Stripe with contributions from the community.
00:02:40.239
Now, how did Sorbet catch my interest? Previously, I had no interest in writing type checks, especially with TypeScript, which seemed cryptic to me. However, while working on a project using TypeScript, I was completely taken aback by how helpful it was to have type checks; they eliminate a lot of mental strain. At that time, I was unaware of the existence of Sorbet. After building a couple of React projects using TypeScript, I decided to search for a type checker for Ruby and stumbled upon Sorbet. What attracted me was how well-maintained it is and the active community surrounding it, along with excellent documentation!
00:03:58.400
Now, with Ruby 3, we have RBS, the type signature language. The engineers at Stripe are incorporating RBS as a way to specify type annotations, in addition to the existing Ruby syntax. As mentioned by Max in his keynote, the Sorbet team is committed to supporting both RBS and RBI, which stands for Ruby Interface, the type signature language that Sorbet employs. This support was only released a couple of weeks ago, so if you're looking to add Sorbet to a running production application, it may present some challenges. My recommendation is to use it within Ruby playgrounds and small codebases rather than large-scale applications.
00:05:24.320
While support for Rails and other significant gems in the ecosystem is still developing, it’s expected due to the recent nature of Sorbet's arrival. There may be some issues with type checking, and certain types may be missing, but using Sorbet can be very beneficial if you use the right objects. I can't think of reasons not to use Sorbet in the Ruby ecosystem.
00:06:17.760
Now, let's move on to a demo. I have a small Ruby file with a couple of classes. In this playground, there are some syntax elements you might recognize that include type checks within strings, which we'll go through.
00:06:35.760
With Sorbet, there are several types of annotations you can specify. For instance, it can check if a method returns an incorrect data type. If you expect a method to return an integer but a string is returned, Sorbet will throw an error. There is also a type called 'false,' which Sorbet ignores in terms of checks, only flagging syntax errors. Similarly, a type labeled 'ignore' will not be checked at all.
00:07:12.959
Moving on, we can specify return types for methods. If SORBET is not set up correctly, for example, a return type that doesn't match the expected type will throw a runtime error. To initialize Sorbet for a Ruby project, you'd use the command line interface (CLI) tool called 'srb.' Running 'srb init' will prepare the project.
00:08:01.280
After initializing, it will create a directory called 'sorbet,' where you'll find RBI files. These files contain decorations related to the terms we use, but keep in mind that Sorbet may not see certain libraries or gems in your application at this time.
00:08:35.080
After setting things up, you can explore how Sorbet can catch errors that occur with specific classes in your Ruby app. For example, when you run code without requiring the Sorbet runtime, you'll encounter an 'uninitialized constant' error because Sorbet's tooling supports only specific types.
00:09:46.560
In this demo, I'm demonstrating how to check types within classes and methods using Sorbet, and I’ll show how returning nil will cause errors. This needs to be managed in Sorbet through correctly specifying types and method returns.
00:10:29.760
You can also declare types for your variables. It's essential to retrieve and declare the data types correctly to ensure no errors are encountered during runtime. I'll illustrate how to declare types for variables and what errors might arise if the types are incorrectly declared.
00:11:32.880
Remember to always specify awkward or custom return types for your methods to prevent unexpected errors during execution. Depending on the complexity of your Ruby application, implementing Sorbet will help ensure code reliability.
00:12:59.600
Moving forward, we can write exceptions using Sorbet that allow checking for nil or wrong data types without resorting to traditional argument errors. Adding type checks can save significant time and effort in coding and testing.
00:14:23.840
Now that we've gone through most of the concepts, if you have any questions or would like me to elaborate on specific parts, feel free to ask.
00:15:46.120
As we wrap up, just remember that you can utilize Sorbet in different environments. I'm looking forward to enhancing projects with type checking. It's crucial to stay updated with any developments in the Sorbet community, especially as Ruby itself evolves.
00:16:56.640
These observations should serve as a valuable guide for you as you engage in your projects that incorporate Sorbet, particularly for the Ruby programming language. Thank you for your time!
00:20:08.000
If you have any additional questions about implementing Sorbet or specific scenarios in production, I would love to discuss that further.
00:22:00.000
Thank you for the kind introduction and for all the questions! Remember to explore the integration of Sorbet into your daily workflow, as it can significantly enhance code accuracy and reliability.