| 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:
class Person
attr_reader :name
attr_reader :age
def initialize(name, age = nil)
@name = name
@age = age
end
end
Method Annotation Styles
1. Primitive #: (Most Concise)
def count(text)
text.length
end
def name
@name
end
def fetch(n = nil)
n ? @items.take(n) : @items.first
end
2. Keyword # @rbs (Structured)
def format(text, width)
text.ljust(width)
end
def double(value)
value * 2
end
3. Doc-Style Parameters (Self-Documenting)
def wrap(text, width: 80, strict: false)
end
Attributes and Variables
attr_reader :name
attr_accessor :count
VERSION = "1.0"
Block Annotations
def each(&block)
@items.each(&block)
end
def process(&block)
yield(42) if block
end
def select(&block)
@items.select(&block)
end
Generic Classes
class Container
def initialize(value)
@value = value
end
def get
@value
end
end
Generic Module Includes
Specify type parameters for generic modules using #[Type]:
include Enumerable
include Comparable
include MyGeneric
Raw RBS for DSLs (# @rbs!)
Use for DSL-generated methods or interface declarations:
Special Annotations
def internal_method; end
def process(data)
super
end
Generate RBS Files
bundle exec rbs-inline lib
bundle exec rbs-inline --output lib
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