RubyMine 2024.2 Help

Document code

YARD and RDoc are the most popular documentation generation tools used in multiple libraries for documenting code. RubyMine allows you to view documentation written in a YARD or RDoc syntax using Quick Documentation Lookup. Moreover, RubyMine provides extended capabilities to work with YARD tags:

  • Create missing YARD tags

  • Check the validity of YARD tags and fix them

  • Utilize YARD tags for better code insight (for example, determining an object type, viewing parameter information for methods, and so on)

Create and fix YARD tags

Here we'll show you how to document a method that takes several parameters. In our case, the method takes two arguments and returns the product of these values.

def multiply(val1, val2) end

We'll see on how to annotate this method using YARD tags and fix incorrect tags.

Add @param tag

  1. Place a caret at the method name and press Alt+Enter.

  2. In the invoked popup, select the Add @param tag action.

  3. RubyMine will add the corresponding comment above the method and will suggest that you specify the type of each parameter value. Start typing Integer and then press Ctrl+Space to view suggestions. Select Integer, press Enter to confirm your choice and press Enter one more time to specify the type of the second parameter.

    Add param tags

Configure syntax format for @param tag

By default, RubyMine generates the parameter name after the parameter type (for example, @param [Integer] val1). To change this behavior and set the parameter name before its type (@param val1 [Integer]), do the following:

  1. Open the Settings dialog (Ctrl+Alt+S).

  2. Open the Editor | Inspections page and select the Method parameter missing '@param' tag inspection in the Ruby group.

  3. Specify the desired order using the Tag syntax format option.

Remove incorrect YARD tags

When you edit YARD tags, RubyMine checks whether or not any wrong tags exist. For example, this can be a duplicated tag or tag does not have a corresponding parameter in code. To remove an incorrect YARD tag:

  1. Place the caret at the highlighted line containing the desired tag and press Alt+Enter.

  2. In the invoked popup, select the Remove tag action.

    Remove a param tag

YARD for code insight

RubyMine utilizes YARD type annotations for various code insight features, for example, determining an object type, using the obtained object type in code completion, viewing parameter information for methods, and so on. Let's see on several examples.

View type info

RubyMine uses YARD tags such as @return, @param, or @yieldparam, to determine object types.

View type info

Note that RubyMine suggests completion results corresponding to the specified type.

Object type in completion

    Return/parameter type mismatch

    RubyMine can check whether or not the actual return and parameter types for a method match corresponding types provided using the @return and @param annotations. For example, if the method return type does not match the @return type, the editor will show you a warning.

    Method return type mismatch

    You can manage these warnings using the Mismatched parameter type and Mismatched return type inspections.

      Parameter info

      RubyMine understands the @overload tag and will suggest to you all the declared overloads when showing parameter information.

      Parameter info

        @type tag

        Apart from standard YARD and RDoc tags, RubyMine can process the @type tag to receive information about the variable type. For example, you can use this tag for local variables (with or without variable names) and block parameters:

        • Local variables

          # @type [Integer] customer_id = 1 # @type [String] customer_name customer_name = "Andrew Fuller"
        • Block parameters

          # @type [Integer] number [1, 2, 3].each do |number| puts "#{number}" end

        Render YARD/RDoc comments

        RubyMine allows you to render YARD/RDoc comments in the editor. Rendered comments are easier to read, and they don't overload your code with extra tags. You can click links to go to the referenced web pages, or view quick documentation for the referenced topics.

        YARD comments in the editing mode
        YARD comments in the rendered mode

        RubyMine also recognizes YARD macro extensions and provides general coding assistance for them. When rendering macros, RubyMine displays the macro data in place of its reference. This includes any variable substitutions that may be used in the macro data ($1, $2, and so on).

        YARD macros in the editing mode
        YARD macros in the rendered mode

        Toogle a rendered view

        1. Click Toggle rendered view in the gutter next to the necessary documentation comment (or press Ctrl+Alt+Q) to toggle the rendered view; click to edit the comment.

        2. (Optional) To change the font size, right-click a comment in the editor and select Adjust Font Size from the context menu. Note that the rendered comments use the same font size as the quick documentation popup.

        Enable rendering comments by default

        You can configure the IDE to always render comments in the editor.

        • Right-click the icon in the gutter (the Toggle Rendered View icon or the Toggle Rendered View icon) and enable the Render All option.

        • Alternatively, in the Settings dialog Ctrl+Alt+S, select Editor | General | Appearance and enable the Render documentation comments option.

        To edit a rendered comment, click the the Toggle Rendered View icon icon in the gutter next to the comment.

        Last modified: 08 October 2024