Learn RubyMine Tutorials

RBS: How to Get the Most Out of RubyMine’s Code Assistance

The following article shows the benefits of investing some of your time into adding RBS declarations to your project.

Catch type errors

RubyMine performs real-time type checking based on the information stored in .rbs files. It will show a type error if there’s a mismatch between the RBS and Ruby code, like in method parameters or return values.

Inspection: type mismatch between Ruby and RBS

There’s also a new inspection for optional types specified in RBS. In such cases, RubyMine will show an error if you try to invoke a method without first checking for nil:

Nil dereference error

Go to declaration and Find usages can help a lot with navigating large code bases, especially those that are new to you.

RubyMine can find potential declarations and usages even without RBS.

However, when RubyMine can rely on RBS declarations, the results are much more precise. This happens because the IDE can determine exactly how project entities relate to each other instead of showing all potential usages:

Go to declaration, with and without using RBS

Understand unfamiliar code

To better understand the structure of an unfamiliar Ruby program, you can jump between corresponding Ruby and RBS files. There are several ways to do that, like using the ⌃⌘↑ (Ctrl+Alt+Home) shortcut.

Naviation between Ruby and RBS

To identify the type of an expression, you can use the Type Info action (⌃⇧P / Ctrl+Shift+P).

Without RBS, RubyMine uses its own type inference to show the type, but in some cases Ruby code does not contain enough information to do that. On the other hand, Type Info always works when RubyMine utilizes the types specified in RBS files.

Type info with and without RBS

Refactor more precisely

The Rename refactoring becomes more precise for code that has corresponding .rbs declarations. When RubyMine utilizes on RBS it will only suggest renaming relevant entities, instead of showing all potential options:

Rename refacroting in RubyMine

Get more relevant code assistance

RubyMine uses RBS signatures to determine the types of Ruby elements and to provide more relevant code completion.

Version 2021.2 adds support for most of the existing RBS entities, including type variables, block parameters, tuple types, proc types, and more.

Improved Ruby code assistance

The features mentioned above will work even if you do not have RBS signatures for all of your code. RubyMine will make use of whatever type signatures are present, so you can add them to your project gradually.

Support for RBS was first introduced in RubyMine 2021.1, and improved in v2021.2.

To start using RBS in RubyMine, you will need the rbs gem added to your project. It comes bundled with Ruby 3.0.0 (or later), but needs to be manually installed for earlier Ruby versions (up to Ruby 2.6.0).

Check out our documentation to learn more about RBS support in RubyMine. And as always, please feel free to report any issues you encounter.

Next up: Features for Working with RBS files in RubyMine

Happy developing!
The RubyMine team

image description