RubyMine 2016.2 Help

Using Annotations

On this page:

Basics

To make your code readable, it a good idea to annotate it. RubyMine provides various annotations, based partly on YARD, partly on JSDoc.

When a user invokes quick documentation, RubyMine displays the information taken from annotations.

Extracting types from the YARD annotations

Consider the following method:

# @return [String ] ef black_box(param1, param2)

The only information about the method is the return type, specified in the YARD tag. This is enough for RubyMine to suggest proper code completion (Ctrl+Space):

ruby_yardReturnType

If the parameter type is declared in the documentation comment, then code completion inside the method body suggests only the relevant choices:

ruby_yardCodeCompetionInMethod

Intention actions suggest to create YARD tags, if missing

If in the same code block, the @return or @param tags are missing, RubyMine provides intention actions (Alt+Enter):

ruby_yardAddReturnTag

In-place rename refactoring

The rename refactoring involves the YARD tags:

ruby_yardRenameParameter

Code inspections and quick fixes

RubyMine validates the tags for duplicated or wrong parameter names:

ruby_yardInspection

Annotating local variables

Local variables can be annotated both with or without the variable names:

# @type [String] my_var = magic_method # @type my_var [String] my_var = magic_method # @type [String] my_var my_var = magic_method # @type [String] my_var Add some documentation here my_var = magic_method

Also, RubyMine supports multiple assignments:

# @type my_var [String] The first part # @type other_var [Array<String>] The second part my_var, other_var = magic_method

Annotating block parameters

method_with_block do # @type [String] param1 # @type [Range] param2 | param1, param2 | # some code... end

Viewing Quick Documentation pop-up

RubyMine properly renders the annotation tags in the quick documentation pop-up window.

For example, this is how RubyMine renders the YARD tags:

ruby_yardQuickDoc

The following image demonstrates how RubyMine shows type annotations:

rm_inferred_types

See Also

Last modified: 30 November 2016