/sorah-guides:sorbetfrom sorah-guides
Provides Sorbet type-checking guidance for Ruby/Rails projects using inline RBS comments. Covers signature syntax, nil/union handling, concern patterns, shim vs cast rules, and known Sorbet gaps with workarounds.
Ships in sorah-guides. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
How to write types in a project that typechecks with Sorbet using inline RBS comments. Signatures live in #: comments that Ruby ignores, so sorbet-runtime is not loaded and nothing about the types runs in production. The project's docs/type-checking.md takes priority over this guide. To introduce Sorbet to a project that does not have it yet, use the sorbet-adoption skill. Libraries and gems typecheck with Steep instead; see the steep skill.
#: RBS comments in .rb files.
sig blocks, extend T::Sig, T.let, T.must, T.cast and T.untyped need sorbet-runtime. Where it is absent they raise NameError at load time. Where a gem pulls it in anyway, they turn into runtime checks nobody asked for..rbi shim files are the exception: they are never loaded, and sig blocks are the correct syntax there.srb tc -a. Its autocorrect inserts exactly the forbidden T. forms.app/, lib/ or config/ starts at # typed: strict. Spec files carry no sigil, because the RSpec DSL does not typecheck.strict, not true. At true, Sorbet requires no signatures, so a file can pass with almost none, and the signatures are what make the types useful to the next person writing a call.bundle exec srb tc from the project directory after every change.srb tc again. Many workarounds turn out to be unnecessary on a retry: a constant cast as untyped before .new, .class.to_s in place of .name, a cast on Time.current.#: (String, expires_in: Integer, ?scopes: Array[String]) -> Token
def issue(subject, expires_in:, scopes: [])
#: (String, readonly: bool, ?actor: User?,
#| ?tags: Array[String]) -> void
def record_event(name, readonly:, actor: nil, tags: [])
#: [T] () { () -> T } -> T
def with_lock(&block)
#: Integer
attr_reader :max_age
DEFAULT_TTL = 300 #: Integer
def initialize(order_id:, amount:)
@order_id = order_id
@amount = BigDecimal(amount) #: BigDecimal
@invoice = nil #: Invoice?
end
#: on the line that assigns them. A #: on the line above does not attach to a constant.initialize, including any that a later step method fills: assign it nil there with a nilable type. Sorbet rejects a non-nilable instance variable first assigned outside initialize (error 5005).@x ||= ... needs a nilable type.#: after a modifier if does not attach to the assignment (7043). Parenthesize the value instead: @cart = (Cart.new(user:) if user) #: Cart?.yields must still declare a block parameter (&block or &_block) before its signature can describe a block (3552).** or * in a class that also contains an RBS #: cast segfaults srb tc. Sorbet also cannot check explicit keywords mixed with a splat (7019), so merge the options into one hash before the call, or spell the keywords out.attr_reader that names several attributes with different types. A multiple assignment cannot carry a #: for each target.#: type payer_like = User | Organization | Guest, and refer to it elsewhere as Owner::payer_like.[Header, Claims] | [nil, nil].Module[untyped], not a bare Module, at strict.#: as annotates the whole right-hand side of a line. It cannot sit in the middle of an expression.#: as T? trips the strict-only useless-cast check. Use a plain #: T? declaration instead.&. on a receiver that Sorbet knows is non-nil is an error (7034). Drop the safe navigation.#: as !nilA #: as !nil cast is a silent T.must. When the invariant behind it breaks, the program fails with a NoMethodError somewhere downstream instead of at the line that made the assumption. Remove the nil instead. Try these in order:
x = nil and assigning x inside the block.case the else raise it is missing when its other arms are already rejected upstream. The method then returns a non-nilable value, and every caller stops asserting.fetch for required keys and indexes. When the input comes from outside, use fetch(0) { raise InvalidToken } so a bad request returns a 4xx rather than a KeyError 500.each_byte, drop(n), first(n), rpartition.if (session = result.session) && session.active?.<name>_maybe.
#: () -> User?
def current_user_maybe = current_session_maybe&.user
#: () -> User
def current_user = Must.because(current_user_maybe) { "gate on require_login! before reading current_user" }
check_prerequisite!, perform) a private raising reader.def slot(value = nil) keeps its plain name for declarations. Add a slot_value reader that raises NotImplementedError, "#{name} declares no slot".Must.because(value) { "invariant" }. The reason names what makes the value present.Must.expect(value). Use the reason-less form only for a non-optional belongs_to reader (tapioca types every belongs_to as nilable), or for a stdlib call whose guard is on the adjacent line.Further rules:
notify_owner!(@owner) beside @owner, or renew(result:, record: result.record).Must.because, which names the caller's guard as the reason. A new parameter is right only when it carries something the callee could not otherwise reach.value&.then { use(it) } instead of a local plus a cast.Narrow a union to one member with Must.be_a(value, Klass), never #: as Klass.
End a case that handles every member of a union with else value #: absurd. Sorbet then reports any member added later that no arm handles. An else raise there is reported as unreachable code.
Sometimes a precise parameter type makes the method's own runtime guard unreachable (7006).
Object or untyped, so the guard stays live. Never delete validation of external input to please the checker.Use untyped only for values that really are heterogeneous:
respond_to?(:call);**options forwarded into a dependency.Never use it to silence an error on a parameter.
Copy this module into a project that lacks it.
app/models/must.rb.lib/<gem>/must.rb as <Gem>::Must.Violation a direct subclass of StandardError, not of the library's error base, so the library never renders it as a domain error.Violation carries the block's message, and that the block is not called when the value is present.# typed: strict
# frozen_string_literal: true
# Runtime counterpart of Sorbet's T.must for a value the type checker cannot
# prove present. The reason names the invariant that makes it so.
module Must
class Violation < StandardError; end
#: [T] (T? value) { () -> String } -> T
def self.because(value, &_block)
case value
when nil then raise Violation, yield
else value
end
end
#: [T] (T? value) -> T
def self.expect(value)
case value
when nil then raise Violation, "expected a value, got nil"
else value
end
end
#: [T] (untyped value, Class[T] klass) -> T
def self.be_a(value, klass)
case value
when klass then value
else raise Violation, "expected #{klass}, got #{value.class}"
end
end
end
Calling value.nil? on an unconstrained type parameter is an error (7038), which is why the module narrows with case. Unlike a redundant cast (7015), a redundant Must call is not reported, so remove stale ones by hand.
module with # @requires_ancestor: ActiveRecord::Base. This needs --enable-experimental-requires-ancestor in sorbet/config, and you can write one line per ancestor.ClassMethods body or a lambda such as default from: -> { ... }, make the first line of the method or lambda body #: self as ApplicationController.
x = self #: as Host)._controller-style accessors.send.self.class with #: as singleton(Host). When the module also needs its own class methods, cast to an intersection: #: as singleton(ActiveRecord::Base) & Foo::ClassMethods.raise, loop or caller_locations, add include Kernel to the module and call the method plainly. Never write Kernel.raise.includes added inside an included do block. Include the module directly in the class, or declare the method in a shim.Foo = Data.define(:a, :b), never class Foo < Data.define(...). Sorbet cannot resolve a computed superclass. To add a constant, reopen the class with class Foo after the assignment.sorbet/rbi/shims/data_define.rbi:
# typed: strict
# Sorbet synthesizes the readers of a `Data.define` class without signatures,
# which a `# typed: strict` file cannot supply inline.
class Checkout::Result
sig { returns(Invoice) }
def invoice; end
end
initialize with positional parameters. A keyword constructor with validation or defaults would then need allocate.tap { send(:initialize, ...) }. Write a plain class instead, with a keyword initialize, readers, ==, eql?, hash and freeze.sorbet/rbi/shims/<gem>.rbi with a one-line comment saying why tapioca cannot see the method. One shim fixes every call site, while a cast fixes one. Ways a gem can hide a method from tapioca:
class_eval in an included hookmethod_missing delegationtapioca gem has runMapper.include(Routing) at load timeTime.current returns ActiveSupport::TimeWithZone whenever config.time_zone is set.tapioca gem after Gemfile.lock changes;tapioca dsl after adding a model, a column or an association.references/known-gaps.md for shims that most Rails apps need.if: ->(record) { record.purpose == ... }. Sorbet types the self of a bare -> { } block as the class, not the record.alias_method :revoked?, :discarded? as def revoked? = discarded? when the target comes from a module. Sorbet resolves aliases before it applies module inclusion.perform methods have no initialize of their own, so their instance variables stay nilable.
Must.because.NOT NULL columns as non-nil, but an unsaved record can still hold nil. A validator that checks presence has to read the column through a local.def revoked? = discarded?), write no comment.**, write one line inside the method body.strict names the Sorbet limitation in its commit message. If the limitation is general, also record it in the project's docs/type-checking.md Known gaps section.Class.new do def initialize(...) end end, or defines def self.name on an anonymous class, leaks into Object for the whole project. The symptom is an unrelated Foo.new reporting keyword or arity errors against Object#initialize, or Class#name appearing private. Give that spec # typed: ignore.references/known-gaps.md: Sorbet error codes and limitations, each with its workaround, plus shims commonly needed in Rails appsWhat SKILL.md sets; muted values are Claude Code's defaults
23 skills in this plugin
/codingThis skill should be used when writing or reviewing code in any language. Provides language-agnostic coding conventions for code quality, comments, error handling, and formatting. Project-specific conventions always take priority./commit-styleThis skill should be used when writing git commit messages, creating git commits, composing pull request titles and descriptions, or when the user asks to "commit", "git commit", "write commit message", "pull request", or "PR description". Provides conventions for subject lines, prefix patterns, body content, and verb choices./dsql-on-railsThis skill should be used when building, extending, or testing a Rails app on Amazon Aurora DSQL, or when moving a Rails app onto DSQL. Use it when writing a transaction, a uniqueness check, a schema change, a spec, a rake task, or CI for such an app, even if the user only says "add a model" or "fix this flaky spec" in such an app. Signs include `aws-sdk-dsql`, `pg-aws_rds_iam`, `aws_rds_iam_auth_token_generator`, a `*.dsql.<region>.on.aws` host, `ActiveRecord::SerializationFailure` with `OC000`/`OC001`, `retryable_transaction`, `non_idempotent_transaction`, `create index async`, `script/pdsql`, or the user mentioning "DSQL", "Aurora DSQL", or "OCC". Provides connection settings and adapter patches, OCC-aware transactions, schema constraints with psqldef, roles, per-developer schemas, RSpec setup, and CI against real clusters. Project-specific conventions always take priority./english-textThis skill should be used when writing English text on behalf of the user, such as GitHub issue/PR descriptions, code review comments, replies to maintainers or contributors, commit message bodies, business emails, or when the user asks to "write in English", "draft a reply", "write a PR description", "file an issue upstream", or "reply to this email". Provides register, phrasing, punctuation, and formatting conventions derived from the author's own GitHub activity, commits, and emails./github-ciThis skill should be used when writing, editing, or reviewing GitHub Actions workflows (`.github/workflows/*.yml`, composite actions in `.github/actions/`), or when setting up CI, a release pipeline, a Docker image build, or gem/crate publishing for a project. Use it whenever the user mentions "GitHub Actions", "CI", "workflow", "release workflow", "trusted publishing", "pin actions", "zizmor", "actionlint", or asks to "add tests to CI", "publish on tag", or "build a container image in CI", even if they never say "workflow". Provides the standard workflow layout (ci / `_test` / release), least-privilege and SHA-pinning rules, release and publishing patterns, and per-language test jobs. Project-specific conventions always take priority./japanese-textThis skill should be used when writing Japanese text, blog posts, diary entries, technical articles, prose, or when the user asks to write in Japanese, compose a blog post, draft a diary entry, or write technical content in Japanese. Provides writing style, orthography, and composition conventions across personal, technical, and corporate contexts.npx claudepluginhub sorah/config --plugin sorah-guidesOther plugins' skills for the same job