Convert Figma logo to code with AI

ruby logorbs

Type Signature for Ruby

1,948
214
1,948
142

Top Related Projects

3,602

A fast, powerful type checker designed for Ruby

1,366

Static type checker for Ruby

Quick Overview

RBS is a language for describing the structure of Ruby programs. It allows developers to write type definitions for Ruby classes and modules, enabling static type checking and improved code documentation. RBS is part of Ruby's official type checking system and is integrated with tools like Steep and TypeProf.

Pros

  • Enhances code documentation and readability
  • Enables static type checking, catching potential errors before runtime
  • Improves IDE support for autocompletion and code navigation
  • Seamlessly integrates with existing Ruby codebases

Cons

  • Learning curve for developers new to static typing in Ruby
  • Requires additional effort to write and maintain type definitions
  • May not cover all dynamic Ruby features comprehensively
  • Limited tooling ecosystem compared to more established type systems

Code Examples

  1. Defining a class with methods and type signatures:
class User
  attr_reader name: String
  attr_reader age: Integer

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

  def adult?: () -> bool
end
  1. Defining a module with generic types:
module Enumerable[T]
  def map: [U] { (T) -> U } -> Array[U]
  def filter: { (T) -> bool } -> Array[T]
end
  1. Defining a method with union types and optional parameters:
class Calculator
  def calculate: (Integer | Float, ?operator: :+ | :- | :* | :/) -> (Integer | Float)
end

Getting Started

To start using RBS in your Ruby project:

  1. Install the RBS gem:

    gem install rbs
    
  2. Create an .rbs file for your Ruby class or module:

    # user.rbs
    class User
      attr_reader name: String
      attr_reader age: Integer
    
      def initialize: (name: String, age: Integer) -> void
      def adult?: () -> bool
    end
    
  3. Run the RBS parser to validate your type definitions:

    rbs parse user.rbs
    
  4. Integrate with type checking tools like Steep or TypeProf for full static type checking capabilities.

Competitor Comparisons

3,602

A fast, powerful type checker designed for Ruby

Pros of Sorbet

  • Faster type checking and runtime performance
  • More extensive type system with features like union types and generics
  • Gradual typing approach, allowing incremental adoption

Cons of Sorbet

  • Steeper learning curve due to more complex type system
  • Requires additional setup and configuration
  • May have compatibility issues with some Ruby gems

Code Comparison

Sorbet:

# typed: strict
extend T::Sig

sig {params(x: Integer, y: String).returns(T::Array[Symbol])}
def example(x, y)
  [x.to_s.to_sym, y.to_sym]
end

RBS:

def example: (Integer x, String y) -> Array[Symbol]

Summary

Sorbet offers a more powerful type system with advanced features and better performance, but comes with a steeper learning curve and additional setup requirements. RBS provides a simpler, more native approach to type checking in Ruby, but may lack some of the advanced features found in Sorbet. The choice between the two depends on project requirements, team expertise, and desired level of type safety.

1,366

Static type checker for Ruby

Pros of Steep

  • Provides type checking and inference for Ruby code
  • Offers more advanced type analysis features
  • Integrates well with existing Ruby projects

Cons of Steep

  • Requires separate type definition files
  • May have a steeper learning curve for beginners
  • Less official support compared to RBS

Code Comparison

Steep type definition:

class User
  @name: String
  @age: Integer

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

RBS type definition:

class User
  attr_reader name: String
  attr_reader age: Integer

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

Both Steep and RBS aim to improve type safety in Ruby projects. Steep offers more advanced type checking capabilities and can infer types in some cases, while RBS provides a standardized type definition syntax that's officially supported by the Ruby core team. Steep may be more suitable for complex projects requiring detailed type analysis, whereas RBS might be preferred for its simplicity and official backing. The choice between the two depends on project requirements and team preferences.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

RBS

RBS is a language to describe the structure of Ruby programs. You can write down the definition of a class or module: methods defined in the class, instance variables and their types, and inheritance/mix-in relations. It also allows declaring constants and global variables.

The following is a small example of RBS for a chat app.

module ChatApp
  VERSION: String

  class User
    attr_reader login: String
    attr_reader email: String

    def initialize: (login: String, email: String) -> void
  end

  class Bot
    attr_reader name: String
    attr_reader email: String
    attr_reader owner: User

    def initialize: (name: String, owner: User) -> void
  end

  class Message
    attr_reader id: String
    attr_reader string: String
    attr_reader from: User | Bot                     # `|` means union types: `#from` can be `User` or `Bot`
    attr_reader reply_to: Message?                   # `?` means optional type: `#reply_to` can be `nil`

    def initialize: (from: User | Bot, string: String) -> void

    def reply: (from: User | Bot, string: String) -> Message
  end

  class Channel
    attr_reader name: String
    attr_reader messages: Array[Message]
    attr_reader users: Array[User]
    attr_reader bots: Array[Bot]

    def initialize: (name: String) -> void

    def each_member: () { (User | Bot) -> void } -> void  # `{` and `}` means block.
                   | () -> Enumerator[User | Bot, void]   # Method can be overloaded.
  end
end

The Target Version

  • The standard library signatures targets the latest release of Ruby. (3.2 as of 2023.)
  • The library code targets non-EOL versions of Ruby. (>= 3.0 as of 2023.)

Installation

Install the rbs gem. $ gem install rbs from the command line, or add a line in your Gemfile.

gem "rbs"

CLI

The gem ships with the rbs command line tool to demonstrate what it can do and help develop RBS.

$ rbs version
$ rbs list
$ rbs ancestors ::Object
$ rbs methods ::Object
$ rbs method Object then

An end user of rbs will probably find rbs prototype the most useful. This command generates boilerplate signature declarations for ruby files. For example, say you have written the below ruby script.

# person.rb
class Person
  attr_reader :name
  attr_reader :contacts

  def initialize(name:)
    @name = name
    @contacts = []
  end

  def speak
    "I'm #{@name} and I love Ruby!"
  end
end

Running prototype on the above will automatically generate

$ rbs prototype rb person.rb
class Person
  @name: untyped

  @contacts: untyped

  attr_reader name: untyped

  attr_reader contacts: untyped

  def initialize: (name: untyped) -> void

  def speak: () -> ::String
end

It prints signatures for all methods, classes, instance variables, and constants. This is only a starting point, and you should edit the output to match your signature more accurately.

rbs prototype offers three options.

  • rb generates from just the available Ruby code
  • rbi generates from Sorbet RBI
  • runtime generates from runtime API

Library

There are two important concepts, environment and definition.

An environment is a dictionary that keeps track of all declarations. What is the declaration associated with String class? An environment will give you the answer.

A definition gives you the detail of the class. What is the type of the return value of gsub method of the String class? The definition for String class knows the list of methods it provides and their types.

The following is a small code to retrieve the definition of the String#gsub method.

require "rbs"

loader = RBS::EnvironmentLoader.new()

# loader.add(path: Pathname("sig"))   # Load .rbs files from `sig` directory
# loader.add(library: "pathname")     # Load pathname library

environment = RBS::Environment.from_loader(loader).resolve_type_names

# ::String
string = RBS::TypeName.new(name: :String, namespace: RBS::Namespace.root)

# Class declaration for ::String
decl = environment.class_decls[string]

# Builder provides the translation from `declaration` to `definition`
builder = RBS::DefinitionBuilder.new(env: environment)

# Definition of instance of String
instance = builder.build_instance(string)

# Print the types of `gsub` method:
puts instance.methods[:gsub].method_types.join("\n")
# Outputs =>
#  (::Regexp | ::string pattern, ::string replacement) -> ::String
#  (::Regexp | ::string pattern, ::Hash[::String, ::String] hash) -> ::String
#  (::Regexp | ::string pattern) { (::String match) -> ::_ToS } -> ::String
#  (::Regexp | ::string pattern) -> ::Enumerator[::String, self]

# Definition of singleton of String
singleton = builder.build_singleton(string)
# No `gsub` method for String singleton
puts singleton.methods[:gsub]

Guides

Community

Here is a list of some places you can talk with active maintainers.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bundle exec rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/rbs.