inline-rbs
Write RBS type annotations directly in Ruby source files using comment syntax. Use when adding types to Ruby code with rbs-inline.
When & Why to Use This Skill
The Inline RBS skill streamlines Ruby development by allowing developers to embed RBS type annotations directly into source code using comment syntax. By leveraging the rbs-inline gem, it eliminates the friction of maintaining separate signature files, enhancing code maintainability and enabling robust static type checking within the Ruby ecosystem.
Use Cases
- Simplified Type Annotation: Add type signatures for methods, attributes, and constants directly above their definitions in Ruby files, keeping types and logic in one place.
- Incremental Type Adoption: Gradually introduce type safety to legacy Ruby projects by enabling the magic comment on a file-by-file basis.
- Documenting Complex Logic: Use structured @rbs comments to define intricate block requirements, generic class parameters, and method overloads for better IDE support.
- Automated Signature Generation: Generate standard RBS files from inline comments using CLI tools to support static analysis engines like Steep or TypeProf.
| name | inline-rbs |
|---|---|
| description | Write RBS type annotations directly in Ruby source files using comment syntax. Use when adding types to Ruby code with rbs-inline. |
Inline RBS Annotations
Write type signatures directly in Ruby source files using special comments. Requires the rbs-inline gem.
Quick Start
Every file needs the magic comment:
# rbs_inline: enabled
class Person
attr_reader :name #: String
attr_reader :age #: Integer?
#: (String, ?Integer?) -> void
def initialize(name, age = nil)
@name = name
@age = age
end
end
Method Annotation Styles
1. Primitive #: (Most Concise)
# Before method
#: (String) -> Integer
def count(text)
text.length
end
# Inline return type
def name #: String
@name
end
# Multiple overloads
#: () -> String?
#: (Integer) -> Array[String]
def fetch(n = nil)
n ? @items.take(n) : @items.first
end
2. Keyword # @rbs (Structured)
# @rbs (String, Integer) -> String
def format(text, width)
text.ljust(width)
end
# Overloading with pipe
# @rbs (String) -> Integer
# | (Integer) -> Integer
def double(value)
value * 2
end
3. Doc-Style Parameters (Self-Documenting)
# @rbs text: String -- Input text
# @rbs width: Integer -- Max width
# @rbs strict: bool -- Strict mode
# @rbs return: Array[String]
def wrap(text, width: 80, strict: false)
# Implementation
end
Attributes and Variables
attr_reader :name #: String
attr_accessor :count #: Integer
# @rbs @items: Array[String] # Instance variable
# @rbs self.@cache: Hash[Symbol, T] # Class instance variable
VERSION = "1.0" #: String # Constant
Block Annotations
# Required block
# @rbs () { (String) -> void } -> void
def each(&block)
@items.each(&block)
end
# Optional block (? before type)
# @rbs () { (Integer) -> void }? -> Integer?
def process(&block)
yield(42) if block
end
# Using boolish for filters
#: () { (String) -> boolish } -> Array[String]
def select(&block)
@items.select(&block)
end
Generic Classes
# @rbs generic T
class Container
# @rbs (T) -> void
def initialize(value)
@value = value
end
#: () -> T
def get
@value
end
end
Generic Module Includes
Specify type parameters for generic modules using #[Type]:
include Enumerable #[String]
include Comparable #[self]
include MyGeneric #[Integer | nil]
Raw RBS for DSLs (# @rbs!)
Use for DSL-generated methods or interface declarations:
# For DSL-generated methods
# @rbs!
# attr_accessor name: String
# attr_accessor email: String?
# For interface methods (expected from mixins)
# @rbs!
# def current_user: () -> User?
# def authorized?: (Symbol) -> bool
Special Annotations
# Skip RBS generation
# @rbs skip
def internal_method; end
# Override inherited method
# @rbs override
def process(data)
super
end
Generate RBS Files
# Generate to stdout
bundle exec rbs-inline lib
# Save to sig/generated
bundle exec rbs-inline --output lib
# Watch for changes
bundle exec rbs-inline --watch lib
Common Pitfalls
- Forgetting magic comment - No RBS without
# rbs_inline: enabled - Instance variable narrowing - Always assign to local first for nil checks
- Optional block syntax -
?goes before type:{ (T) -> U }? - Return type mismatch - Ensure signatures match nil possibilities