Ali Ilman

Summarized using AI

Type Checking with Sorbet

Ali Ilman • August 21, 2020 • online

In this talk at the #NoRuKo conference, Ali Ilman, a software engineer from Malaysia, introduces Sorbet, a type checker for Ruby that offers a gradual approach to type checking, making it accessible for Ruby developers. He emphasizes that Sorbet allows developers to incrementally add types throughout their codebase without needing to rewrite everything at once.

The main points discussed in the video include:

  • Introduction to Type Checking: Ali shares his background in software development and his transition from TypeScript to Ruby. He highlights the importance of type checking, especially after gaining experience with TypeScript, which reduced his mental workload during coding.

  • Overview of Sorbet: Sorbet is presented as a well-maintained type checker developed by Stripe, which is compatible with Ruby syntax. Unlike TypeScript, Sorbet enables type checking incrementally, meaning developers can annotate their code at their own pace.

  • Integration with Ruby 3’s RBS: Ali discusses Ruby 3’s introduction of RBS, a type signature language, and how it relates to Sorbet. He explains that the Sorbet team supports both RBS and its own interpretation, RBI, although developers should approach integrating Sorbet into production applications with caution.

  • Demo: A demo illustrates how to set up Sorbet in a Ruby project using the command line tool 'srb'. He walks through the process of adding type annotations and how Sorbet catches specific type errors, reinforcing the necessity of declaring data types correctly to avoid runtime errors.

  • Practical Usage and Benefits: Ali covers how Sorbet can assist with checking method return types, handling nil values, and declaring types for variables. He emphasizes the savings in coding and debugging time that can be achieved through effective type checking.

  • Concluding Remarks: Ali wraps up by encouraging developers to explore Sorbet and stay informed about its community developments. He leaves the audience with the importance of integrating Sorbet into their Ruby projects to enhance reliability and maintainability of code.

Overall, the session serves as both an introduction to Sorbet and a guide for Ruby developers looking to implement type checking in their applications, providing valuable insights and practical demonstrations.

Type Checking with Sorbet
Ali Ilman • August 21, 2020 • online

If you enjoy building projects using TypeScript, you probably wish you could build Ruby projects with a type checker. I've been in that boat! Recently, I stumbled upon Sorbet, a well-maintained type checker for Ruby. In this talk, we'll take a look at how we can use Sorbet.

Ali Ilman is a software engineer from Malaysia. He works for Suria Labs as a full-stack developer. He writes Ruby and JavaScript and other than these technologies, he's getting his hands dirty with Docker and Kubernetes. He loves TDD and service objects. In his free time, he tends to have his head buried in a book.

Welcome to the #NoRuKo conference. A virtual unconference organized by Stichting Ruby NL.

#NoRuKo playlist with all talks and panels: https://www.youtube.com/playlist?list=PL9_A7olkztLlmJIAc567KQgKcMi7-qnjg

Recorded 21th of August, 2020.
NoRuKo website: https://noruko.org/
Stichting Ruby NL website: https://rubynl.org/

NoRuKo 2020

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.
Explore all talks recorded at NoRuKo 2020
+8