RubyMine 2023.3 Help

RBS

RubyMine supports the RBS language to describe the types used in Ruby programs. RubyMine recognizes .rbs files and provides the following coding assistance:

RBS provides a set of built-in type signatures for the standard libraries out of the box. When you use RBS in your project, RubyMine displays its sources in external libraries.

RubyMine can also work with your rbs_collection.lock.yaml file. In this scenario, RubyMine will determine which type signatures will be loaded and used for code insight. For more information, refer to the documentation.

RBS type signatures

RubyMine supports RBS type signatures and uses them to determine the type of Ruby elements. This helps you to experience better code insight when developing your code in Ruby.

If you have RBS files in your project, RubyMine provides:

  • Better method completion

    RubyMine displays methods from appropriate classes at the beginning of the list, when invoking code completion.

    Method name completion with RBS
  • Extra declarations from RBS in Ruby completion

    RubyMine recognize extra declarations from RBS when they are absent in your Ruby files. This is particularly beneficial for projects that extensively use reflection, as it allows for these declarations to still be present in completion. The feature is available for classes, modules, constants, methods, and variables.

    Extra declarations from RBS
  • Improved navigation actions

    When you invoke the 'Go to declaration' action, RubyMine navigates you to the exact declaration instead of providing a list of declarations to choose from.

  • Search for usages of the corresponding declaration

    With RBS types, RubyMine can find the correct declaration of a Ruby element. When you invoke the 'Refactor' action, RubyMine instantly finds usages of the corresponding declaration instead of giving you a list of declarations to choose from.

    Find usages with RBS
  • Consistent rename refactorings

    When RubyMine utilizes RBS, it will only suggest renaming relevant entities instead of showing all potential options.

    Rename refactoring with RBS

View the type info

  • Having RBS signatures provided, RubyMine uses them to determine types of Ruby elements. To view the element type, place the caret at the desired Ruby element and go to View | Type Info Ctrl+Shift+P in the main menu.

    Viewing type info

View the parameter info

RubyMine uses RBS signatures to display types of method parameters in the Parameter info popup. By default, RubyMine shows the popup automatically after you type an opening bracket in the editor, or select a method from the list of suggestions.

  • To invoke the Parameter info popup explicitly, go to View | Parameter Info Ctrl+P in the main menu.

    View Parameter Info

Check the types match

  • RubyMine warns you when you try to use types that don't agree with the types from RBS files.

    Incompatible parameter type

    You can manage these warnings using the Mismatched type inspections. The inspections are available for:

    • Constant assignments

    • Assignments to instance and class variables (including instance variables in singleton classes/methods)

    • Parameter assignments in methods and blocks

    • Return values of methods and blocks

    • Arguments in methods and yield calls

View Quick Documentation

You can use Quick Documentation to get information about RBS signatures in use.

  1. Hover over the necessary symbol in the editor.

  2. Place the caret at the symbol and press Ctrl+Q or select View | Quick Documentation from the main menu.

    Press Ctrl+Q again to open this documentation in the Documentation> tool window.

  3. Click the Show Options Menu icon in the popup to change the font size, display the quick documentation toolbar, or go to the source code.

Create an RBS file

  1. In the Project view Alt+1, select the directory where you want to create a new RBS file.

  2. Press Alt+Insert and select RBS File/Class from the popup.

    Create a new RBS file

    As another option, select File | New | RBS File/Class from the main menu.

  3. Specify the filename and press Enter.

Generate an RBS prototype

RubyMine provides a tool to generate simple signature prototypes from your Ruby code. These prototypes may help to figure out how to start adding new RBS types to your project.

  1. Open a Ruby file, for which you want to generate type signatures.

  2. In the main menu, go to Code | Generate Alt+Insert.

  3. Select Type Signatures in the Generate popup.

    Generate an RBS prototype

    RubyMine will generate a new RBS file with the same name as your Ruby file. All types included in the resulting prototype are specified as untyped. You can use them as templates for writing type signatures.

Add a missing type signature using a quick-fix

You can always add a missing type signature using the quick-fix for the Missing Type Signature inspection. RubyMine will navigate you to the existing RBS file, or create a new RBS file if necessary, and generate a respective signature template.

  1. Place the caret at the Ruby symbol that is missing a type signature.

  2. Press Alt+Enter or click the the Intention action button icon at the line with the problem.

    Add a missing type signature
  3. Select the Add type signature fix from the menu.

  4. In the RBS file that opens, update the type signature.

Edit a type signature

RubyMine allows you to edit type signatures from your Ruby file using the dedicated intention action or the quick-fix available for the Mismatched Type inspection.

  1. Place the caret at the Ruby symbol, which type signature you want to edit.

  2. Press Alt+Enter and select Edit type signature from the menu.

    Edit a type signnature using an intention
    Edit a type signnature using a quick-fix
  3. In the RBS file that opens, update the type signature.

View the structure of an RBS file

You can examine the high-level structure of an RBS file using the Structure tool window Alt+7.

Structure tool window

Navigate between Ruby and RBS files

RubyMine enables you to navigate from a Ruby declaration to its type signature, and vice versa. Navigation features are available for:

  • Attributes (currently works only when navigating from RBS to Ruby)

  • Classes, modules, and constants

  • Class and instance variables

  • Global variables

  • Methods (including singleton)

Go to the type signature

  • Select a Ruby declaration and go to Navigate | Type Signature to navigate to its type signature.

    Navigating between a Ruby declaration and its type signature

Go to the associated Ruby declaration

  • Select a type signature and go to Navigate | Associated Declaration to navigate to the associated Ruby declaration.

    Navigating between an RBS signature and an associated Ruby declaration

Navigate with intention actions

  1. Place the caret at the required element and press Alt+Enter.

  2. Select the action to navigate to the required type signature or associated declaration from the list. Press Enter to apply the intention action.

    Navigate with intention actions

Navigate to a related Ruby or RBS element

  1. Place the caret at the required element and go to Navigate | Related Symbol Ctrl+Alt+Home in the main menu.

  2. Select the desired target from the popup if required.

    Navigate to a related Ruby element

Check Ruby types using Steep

RubyMine supports running Steep commands using Run anything. Before working with Steep, make sure to add the steep gem to your Gemfile:

gem 'steep'

After installing Steep, you need to generate a Steepfile for your project.

Generate and configure a Steepfile

  1. Press Ctrl twice.

  2. Start typing steep init in the search field. Select the command from the list of suggestions and press Enter. RubyMine will generate a Steepfile for your project.

  3. Open and edit the generated Steepfile. For example:

    target :lib do check "lib" signature "sig" library "set", "pathname" end

Run type checking

  1. Press Ctrl twice.

  2. Start typing steep check in the search field. Select the command from the list of suggestions and press Enter.

    RubyMine will display the command output in the Run tool window.

    Command output in the Run tool window

Configure code style settings

RubyMine applies the same code style settings for both Ruby and RBS. To customize the code style for RBS:

  1. In the Settings dialog (Ctrl+Alt+S) , go to Editor | Code Style | Ruby.

  2. Configure the desired code style settings.

Configure syntax highlighting

RubyMine uses the Ruby color scheme to highlight the syntax in RBS files.

You can configure RBS-aware syntax highlighting according to your preferences and habits.

  1. In the Settings dialog (Ctrl+Alt+S) , go to Editor | Color Scheme | Ruby.

  2. Select the color scheme, accept the highlighting settings inherited from the defaults or customize them as described in Configure colors and fonts.

Last modified: 04 April 2024