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.
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:
Navigate large code bases
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:
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.
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.
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:
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.
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).
The RubyMine team
Subscribe to Blog updates
Thanks, we've got you!
Collaborative Programming in RubyMine
Hi everyone, We're back with a new how2pro blog post unveiling another cool RubyMine feature – the Code With Me plugin. When you're knee-deep in a complex task or tangling with an issue that's testing your limits, having someone by your side can make all the difference. Additionally, in an educati…
Working With RBS in RubyMine
Starting with v2021.2, RubyMine provides code assistance for working with RBS files, along with all of the essential features that make RBS code easier to read. Here’s a brief overview. Creating RBS code Code completion Formatting and readability Navigation Refactoring Inspections Do y…
How to Run Ruby/Rails Commands from the IDE instead of the Terminal
In the Ruby/Rails community, using a terminal is the most popular way to perform various commands and tasks. These tasks include running scripts, tests, Rails generators, Git and Docker commands, and many other things. We also noticed this ourselves when we did some RubyMine usability testing among …
Find usages in RubyMineEarly Access Program
To get the most recent information about Searching for usages in RubyMine, refer to the Search for usages topic. Find usages can be used to find all the pieces of code referencing a specific element, such as a class, method, symbol, or many of the others. RubyMine can also find dynamic usages, in…