RBI provides a Ruby API to build, parse, print, format, rewrite, and merge Ruby Interface files consumed by Sorbet.
rbi requires Ruby 3.3 or newer.
Add this line to your application's Gemfile:
gem "rbi"
And then execute:
bundle install
Or install it yourself as:
gem install rbi
require "rbi"
rbi = RBI::File.new(strictness: "true") do |file|
file << RBI::Module.new("Foo") do |mod|
mod << RBI::Method.new("foo")
end
end
puts rbi.string
will produce:
# typed: true
module Foo
def foo; end
end
require "rbi"
tree = RBI::Parser.parse_string(<<~RBI)
class Foo
def bar; end
end
RBI
puts tree.nodes.first.fully_qualified_name # => ::Foo
require "rbi"
tree = RBI::Parser.parse_string(<<~RBI)
class Foo
def bar; end
end
RBI
formatter = RBI::Formatter.new(add_sig_templates: true, group_nodes: true, sort_nodes: true)
formatter.format_tree(tree)
puts tree.string
will produce:
class Foo
# TODO: fill in signature with appropriate type information
sig { returns(::T.untyped) }
def bar; end
end
require "rbi"
left = RBI::Parser.parse_string(<<~RBI)
class Foo
def a; end
end
RBI
right = RBI::Parser.parse_string(<<~RBI)
class Foo
def b; end
end
RBI
puts left.merge(right).string
will produce:
class Foo
def a; end
def b; end
end
require "rbi"
type = RBI::Type.parse_string("T.nilable(String)")
puts type # => ::T.nilable(String)
puts type.rbs_string # => String?
require "rbi"
tree = RBI::Parser.parse_string(<<~RBI)
#: (String) -> Integer
def foo(name); end
RBI
tree.translate_rbs_sigs!
puts tree.string
will produce:
sig { params(name: String).returns(Integer) }
def foo(name); end
require "rbi"
file = RBI::File.new do |f|
f << RBI::Class.new("User") do |klass|
klass << RBI::Method.new("name", sigs: [RBI::Sig.new(return_type: "String")])
end
end
puts file.rbs_string
will produce:
class User
def name: -> String
end
RBI::RBSPrinterAfter checking out the repo, run bin/setup to install dependencies.
Useful commands:
bin/test runs the test suitebin/typecheck runs Sorbetbin/style runs RuboCopbin/console starts an interactive promptversion.rbbundle install to update the version number in Gemfile.lockBump version to vx.y.zgit tag vx.y.zgit push origin vx.y.zWe have a release workflow that will publish your new gem version to rubygems.org via Trusted Publishing. This workflow must be approved by a member of the Ruby and Rails Infrastructure team at Shopify before it will run. Once it is approved, it will automatically publish a new gem version to rubygems.org and create a new GitHub release.
Bug reports and pull requests are welcome on GitHub at https://github.com/Shopify/rbi. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in this project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
23 followers · starred Feb 2024
331 followers · starred May 2022
Ruby
99.9%
RBI provides a Ruby API to build, parse, print, format, rewrite, and merge Ruby Interface files consumed by Sorbet.
rbi requires Ruby 3.3 or newer.
Add this line to your application's Gemfile:
gem "rbi"
And then execute:
bundle install
Or install it yourself as:
gem install rbi
require "rbi"
rbi = RBI::File.new(strictness: "true") do |file|
file << RBI::Module.new("Foo") do |mod|
mod << RBI::Method.new("foo")
end
end
puts rbi.string
will produce:
# typed: true
module Foo
def foo; end
end
require "rbi"
tree = RBI::Parser.parse_string(<<~RBI)
class Foo
def bar; end
end
RBI
puts tree.nodes.first.fully_qualified_name # => ::Foo
require "rbi"
tree = RBI::Parser.parse_string(<<~RBI)
class Foo
def bar; end
end
RBI
formatter = RBI::Formatter.new(add_sig_templates: true, group_nodes: true, sort_nodes: true)
formatter.format_tree(tree)
puts tree.string
will produce:
class Foo
# TODO: fill in signature with appropriate type information
sig { returns(::T.untyped) }
def bar; end
end
require "rbi"
left = RBI::Parser.parse_string(<<~RBI)
class Foo
def a; end
end
RBI
right = RBI::Parser.parse_string(<<~RBI)
class Foo
def b; end
end
RBI
puts left.merge(right).string
will produce:
class Foo
def a; end
def b; end
end
require "rbi"
type = RBI::Type.parse_string("T.nilable(String)")
puts type # => ::T.nilable(String)
puts type.rbs_string # => String?
require "rbi"
tree = RBI::Parser.parse_string(<<~RBI)
#: (String) -> Integer
def foo(name); end
RBI
tree.translate_rbs_sigs!
puts tree.string
will produce:
sig { params(name: String).returns(Integer) }
def foo(name); end
require "rbi"
file = RBI::File.new do |f|
f << RBI::Class.new("User") do |klass|
klass << RBI::Method.new("name", sigs: [RBI::Sig.new(return_type: "String")])
end
end
puts file.rbs_string
will produce:
class User
def name: -> String
end
RBI::RBSPrinterAfter checking out the repo, run bin/setup to install dependencies.
Useful commands:
bin/test runs the test suitebin/typecheck runs Sorbetbin/style runs RuboCopbin/console starts an interactive promptversion.rbbundle install to update the version number in Gemfile.lockBump version to vx.y.zgit tag vx.y.zgit push origin vx.y.zWe have a release workflow that will publish your new gem version to rubygems.org via Trusted Publishing. This workflow must be approved by a member of the Ruby and Rails Infrastructure team at Shopify before it will run. Once it is approved, it will automatically publish a new gem version to rubygems.org and create a new GitHub release.
Bug reports and pull requests are welcome on GitHub at https://github.com/Shopify/rbi. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the code of conduct.
The gem is available as open source under the terms of the MIT License.
Everyone interacting in this project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the code of conduct.
23 followers · starred Feb 2024
331 followers · starred May 2022
Ruby
99.9%