Skip to content

Converting between RBI and RBS

Aaron Christiansen edited this page Sep 13, 2020 · 1 revision

Overview

You can convert a tree of type information between Parlour's formats. This means that you can write code to generate RBI files, and then use a converter to also get an RBS file describing the same types.

Conversions are usually lossy. RBI and RBS come from two very different type systems and it isn't possible to express many of one's concepts in the other. You'll get warnings printed if a significantly lossy operation occurs, such as:

  • A node and its children being entirely dropped because it isn't supported in the target type system.
  • A node being converted one-way into a close equivalent on the target type system (such as an RBI struct being converted to an equivalent RBS class).

Currently, only converting RBI to RBS is supported.

Performing a Conversion from RBI to RBS

  1. If you're using legacy string types, rather than Parlour::Types types, convert your type tree to use Parlour::Types types with Namespace#generalize_from_rbi!.
  2. Create a Parlour::RbsGenerator to insert the converted nodes into.
  3. Create a Parlour::Conversion::RbiToRbs converter instance.
  4. Call #convert_all, passing the root of the RBI namespace to convert from, and the root of the RBS tree to add new children to.

Here's a code example:

rbi_gen = Parlour::RbiGenerator.new
# ...create some types...

# 1. If you used string types...
rbi_gen.root.generalize_from_rbi!

# 2. Create a new RBS generator
rbs_gen = Parlour::RbsGenerator.new

# 3. Create a converter
converter = Parlour::Conversion::RbiToRbs.new(rbs_gen)

# 4. Perform the conversion
converter.convert_all(rbi_gen.root, rbs_gen.root)
Clone this wiki locally