Skip to main content
Examples Verified (100%)

T-Ruby vs Others

The Ruby ecosystem has several approaches to static typing. This page compares T-Ruby with other solutions to help you understand where T-Ruby fits in.

Quick Comparison

FeatureT-RubyRBSSorbetTypeScript
LanguageRubyRubyRubyJavaScript
Type syntaxInlineSeparate filesComments + sigInline
CompilationYes (.trb → .rb)N/ANoYes (.ts → .js)
Runtime checkingNoNoOptionalNo
Gradual typingYesYesYesYes
Generic typesYesYesYesYes
Learning curveLow (TypeScript-like)MediumHigher-

T-Ruby vs RBS

RBS is Ruby's official type signature format, introduced in Ruby 3.0.

RBS Approach

Types are written in separate .rbs files:

lib/user.rb
class User
def initialize(name, age)
@name = name
@age = age
end

def greet
"Hello, I'm #{@name}"
end
end
sig/user.rbs
class User
@name: String
@age: Integer

def initialize: (String name, Integer age) -> void
def greet: () -> String
end

T-Ruby Approach

Types are written inline:

lib/user.trb
class User
@name: String
@age: Integer

def initialize(name: String, age: Integer): void
@name = name
@age = age
end

def greet: String
"Hello, I'm #{@name}"
end
end

Key Differences

AspectT-RubyRBS
Type locationSame file as codeSeparate .rbs files
SynchronizationAutomaticManual (can drift)
ReadabilityTypes visible while codingNeed to check two files
Generated output.rb + .rbs.rbs only

When to choose RBS:

  • You can't modify the Ruby source files
  • Working with third-party libraries
  • Team prefers separation of concerns

When to choose T-Ruby:

  • You want types co-located with code
  • Coming from TypeScript
  • Want automatic RBS generation

T-Ruby vs Sorbet

Sorbet is a type checker developed by Stripe.

Sorbet Approach

Types use sig blocks and T:: syntax:

lib/calculator.rb
# typed: strict
require 'sorbet-runtime'

class Calculator
extend T::Sig

sig { params(a: Integer, b: Integer).returns(Integer) }
def add(a, b)
a + b
end

sig { params(items: T::Array[String]).returns(String) }
def join(items)
items.join(", ")
end
end

T-Ruby Approach

lib/calculator.trb
class Calculator
def add(a: Integer, b: Integer): Integer
a + b
end

def join(items: Array<String>): String
items.join(", ")
end
end

Key Differences

AspectT-RubySorbet
Syntax styleTypeScript-likeRuby DSL
Runtime dependencyNonesorbet-runtime gem
Runtime checksNoOptional
CompilationRequiredNot required
VerbosityLowerHigher

Sorbet example with runtime checks:

# Sorbet can check types at runtime
sig { params(name: String).returns(String) }
def greet(name)
"Hello, #{name}"
end

greet(123) # Raises TypeError at runtime if runtime checks enabled

T-Ruby approach:

# Types are compile-time only
def greet(name: String): String
"Hello, #{name}"
end

greet(123) # Compile error (caught before running)

When to choose Sorbet:

  • You need runtime type checking
  • Already using Sorbet in your project
  • Prefer not to have a compilation step

When to choose T-Ruby:

  • Want cleaner, more readable syntax
  • Don't want runtime dependencies
  • Coming from TypeScript

T-Ruby vs TypeScript

Since T-Ruby is inspired by TypeScript, let's see how they compare:

Syntax Comparison

TypeScript
function greet(name: string): string {
return `Hello, ${name}!`;
}

interface User {
name: string;
age: number;
}

function processUser<T extends User>(user: T): string {
return user.name;
}
T-Ruby
def greet(name: String): String
"Hello, #{name}!"
end

interface User
def name: String
def age: Integer
end

def process_user<T: User>(user: T): String
user.name
end

Similarities

  • Inline type annotations
  • Type erasure (no runtime overhead)
  • Gradual typing support
  • Union types (String | Integer)
  • Generic types (Array<T>)
  • Interface definitions

Differences

AspectT-RubyTypeScript
Base languageRubyJavaScript
Type namingPascalCase (String, Integer)lowercase (string, number)
Null typenilnull, undefined
OptionalT? or T | nilT | null or T?
Method syntaxRuby's defFunction expressions

Integration: Using Multiple Tools

T-Ruby generates RBS files, which means you can use it alongside other tools:

┌─────────────┐
│ .trb │
│ files │
└──────┬──────┘
│ trc compile

┌─────────────┐ ┌─────────────┐
│ .rb │ │ .rbs │
│ files │ │ files │
└─────────────┘ └──────┬──────┘

┌──────────────┼──────────────┐
▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌──────────┐
│ Steep │ │ Ruby LSP │ │ Sorbet │
│ checker │ │ IDE │ │(optional)│
└──────────┘ └──────────┘ └──────────┘

Recommendation

If you...Consider
Are starting a new Ruby projectT-Ruby - clean syntax, good DX
Have an existing Sorbet codebaseSorbet - avoid migration cost
Need runtime type checkingSorbet - built-in support
Want types separate from codeRBS - official format
Come from TypeScriptT-Ruby - familiar syntax
Work on a large teamT-Ruby or Sorbet - both work well

Conclusion

T-Ruby offers a modern, TypeScript-inspired approach to Ruby types with:

  • Clean, inline syntax
  • Zero runtime overhead
  • Automatic RBS generation for ecosystem compatibility

It's not about replacing RBS or Sorbet—it's about giving Ruby developers another option that might better fit their workflow and preferences.