RubyMine 2018.3 Help

Generating Code

Complete paired elements

RubyMine can automatically add various closing elements to your code, for example, brackets, quotes, XML and HTML tags, or tags that wrap Ruby code within views (<% %>).

To enable or disable whether to add closing elements, open the Settings/Preferences dialog (Ctrl+Alt+S ), click General under Editor, and then Smart Keys. For example, you can use the following options:

  • Insert pair brackets

  • Insert pair quote

Generate methods from usage

You can generate an empty stub for a method that you want to use but have not yet defined.

To do this, perform the following steps:

  1. Type a name that references a non-existent method. RubyMine highlights the reference.

  2. Press Alt+Enter, choose the Create method '<name>' from the suggestion list, and press Enter.

Create attributes

RubyMine lets you quickly add accessor methods for instance variables using the attr_reader, attr_writer, and attr_accessor attributes.

To do this, perform the following steps:

  1. Place a caret inside a class and press Alt+Insert.

  2. In the popup that opens, select Reader, Writer, or Accessor.

  3. Choose the required instance variable in the invoked dialog and click OK.

Override methods of a superclass

You can override any method of a parent class by generating the corresponding method stub in a child class.

Perform the following steps to do this:

  1. Place a caret inside a child class an do one of the following:

    • On the Code menu, click Override methods (Ctrl+O).

    • On the Code menu, click Generate (Alt+Insert) and select Override methods....

  2. Choose the desired method in the Select Methods to Override dialog.

  3. Implement the created method.

Surround code fragments with language-specific constructs

RubyMine provides standard templates for surrounding code fragments with various constructs based on the language of the source code. For example, you can wrap your code into the if … end or unless … end conditional statements.

To surround a block of code:

  1. Select the desired code fragment.

  2. On the Code menu, click Surround With (Ctrl+Alt+T).

  3. Select the necessary surround statement from the list and press Enter.

Unwrap and remove statements

RubyMine lets you quickly unwrap or extract expressions from enclosing statements.

This action is available for:

  • Ruby

  • JavaScript

  • XML and HTML tags

To unwrap or remove a statement:

  1. Place the caret on the expression you want to extract or unwrap.

  2. Choose Code | Unwrap/Remove on the main menu or press Ctrl+Shift+Delete. RubyMine shows a pop-up window with all the actions that are available in the current context.

  3. Click the desired action and press Enter.

Create code constructs using live templates

Live templates can be used to insert frequently-used constructs into your source code, for example, loops, conditions, various declarations, print statements, tags, and so on.

To learn more, see Using Live Templates.

Last modified: 6 December 2018