Sorbet in Rails: Your Bug Radar Before Production Hits
Posted at 23-July-2025 / Written by Rohit Bhatt

30-sec summary
Ruby is expressive, fun, and fast to write. But it's also... wild. With great freedom comes the risk of accidentally breaking things. Sorbet brings some guardrails to your Rails app. It’s a static type checker built just for Ruby — and this post is your beginner-friendly map to using it effectively in a Rails app.
Why Should You Care About Typing in Ruby?
Imagine calling a method expecting an integer and accidentally passing a string. Ruby will let you do it — until runtime — when it crashes. Static typing helps catch those mistakes *before* your code even runs. That’s what Sorbet does. It doesn’t take away Ruby’s flexibility but lets you add helpful type hints, making bugs easier to catch and code easier to understand.
What is Sorbet, Really?
Sorbet is a static type checker made by Stripe. It lets you describe what types your methods expect and return. It’s like telling your future self (and your teammates), ‘Hey, this method always gives back a String.’ You can keep using Ruby like normal, but Sorbet will sit quietly in the background, keeping an eye out for mismatches.
Getting Sorbet Up and Running in Rails
- 1.1. Add the Gems: Inside your Gemfile, add: ```ruby gem 'sorbet' gem 'sorbet-runtime' gem 'sorbet-rails' # helps with Rails model types ```
- 2.2. Bundle it up: Run `bundle install` to get those gems installed.
- 3.3. Set Up Sorbet: Run: ```bash bin/tapioca init # sets up RBI generation srb init # creates the Sorbet folders ```
- 4.4. Generate Type Definitions: Let Sorbet learn about your app’s structure: ```bash bin/tapioca generate ```
Annotating Your Code
Now comes the fun part — writing code that talks back. Here’s how to annotate your methods:
1class User < ApplicationRecord
2 extend T::Sig
3
4 sig { returns(String) }
5 def full_name
6 "#{first_name} #{last_name}"
7 end
8
9 sig { params(age: Integer).returns(T::Boolean) }
10 def eligible?(age)
11 age >= 18
12 end
13end
File-Level Strictness — You’re In Control
At the top of every file, you can choose how strict Sorbet should be. This means you can start with training wheels and slowly go full strict ninja mode:
- 1.# typed: false: Ignore this file completely.
- 2.# typed: true: Basic safety checks. A good starting point.
- 3.# typed: strict: Now we’re serious. Everything must be typed.
- 4.# typed: strong: Even more aggressive. Requires clean and consistent codebases.
Things That Might Trip You Up
- 1.Method Signatures Everywhere: It’s more typing (literally), but those few lines save hours of debugging.
- 2.Dynamic Ruby is Hard for Sorbet: Metaprogramming is tricky for static checkers. If needed, use `T.unsafe` or generate RBIs manually with `tapioca`.
- 3.RBI Files Can Get Stale: If you change gem versions or method signatures, regenerate RBI files. A quick `tapioca generate` keeps things fresh.
Wrapping Up
Sorbet helps Rails apps grow with confidence. You won’t need to switch your style or rewrite everything — just add type hints gradually. If you’re just starting out in Ruby, Sorbet is like a second pair of eyes watching your back. And once you get the hang of it, you’ll wonder how you shipped code without it.