RubyMine 2019.2 Help

Getting started guide

RubyMine is an integrated development environment (IDE) that helps you be more productive in every aspect of Ruby/Rails projects development - from writing and debugging code to testing and deploying a completed application. RubyMine is available for different platforms including macOS, Windows, and Linux.

In this tutorial, we’ll show you the main RubyMine capabilities using a fork of a sample application created for the Ruby on Rails Tutorial. Before starting this tutorial, do the following:

We'll perform all steps using RubyMine installed on macOS.

Open a project

First of all, we need to clone the repository containing the sample application:

  1. Run RubyMine, click Check out from Version Control on the Welcome Screen and select Git.

    Welcome Screen / Git
  2. In the Clone Repository dialog, insert the following address to the URL field and click the Clone button: https://bitbucket.org/rubyminedoc/sample_rails_app.git.

    Clone Repository

    RubyMine will show a progress bar indicating a cloning process.

    Cloning source repository
  3. After cloning the repository, you will be prompted to open the directory containing the project. Click Yes.

    Checkout From Version Control
  4. RubyMine opens the directory and starts the indexing process. You can see the progress in the Status Bar.

    Indexing

    RubyMine indexes your project to analyze its sources and collect the information on available files, class and function definitions, etc. This is required for code insight features code completion and navigation.

Select Ruby interpreter and install dependencies

After you’ve opened the project, it is necessary to select the required Ruby interpreter and install the dependencies specified in the project's Gemfile:

  1. Press Ctrl+Alt+S to open the Settings/Preferences dialog, go to the Languages & Frameworks | Ruby SDK and Gems page and choose the required interpreter.

    Select Ruby interpreter

    Click OK.

  2. Now, let’s install the gems specified in the Gemfile. RubyMine allows you to use Bundler to manage gems. To install Bundler, press Ctrl twice and enter the following command: gem install bundler.

    Run Anything / gem install bundler

    Press Enter and wait until the Bundler gem is installed.

  3. Now you can use Bundler commands within RubyMine. To install gems, press Ctrl twice again and start typing bundle install. Then, select the bundle install command from the list and press Enter.

    Run Anything / bundle install
  4. In the Bundle Install dialog, click Install.

    Bundle Install dialog
  5. Wait until RubyMine installs all gems.

    Run Tool window / installing gems

    Now we can try some features in the editor.

RubyMine provides rich navigation capabilities to explore projects of any sizes. You can navigate between files, go to declarations, search entities of any types, etc.

Project view

The Project view on the left side of the IDE displays the project structure. You can use it to open any file in your project, create new files, and so on.

Project view

Go to declaration

Go to declaration allows you to navigate to the declaration of a symbol from any symbol usage. To see this capability in action, press Ctrl+Shift+N, start typing users_controller, select the users_controller.rb file and click Enter.

Go to file

In the opened app/controllers/users_controller.rb file, place the caret next to the User class and press Ctrl+B.

You'll jump to the class declaration in the user.rb file.

Note that you can jump not only to project entities but to definitions within external libraries (which are gems in our case). For instance, keeping Ctrl ( for macOS) pressed, hover your mouse pointer over the has_many method. When the method turns into a hyperlink, click it without releasing the key.

RubyMine will open the method definition within the ActiveRecord Rails module.

Find usages

Let’s demonstrate the Find usages feature. In the app/controllers/users_controller.rb file, scroll down to the edit action, place the caret next to it and press Alt+F7. In the Find window, you can explore the places where this action is used.

Find usages

When working on a specific Rails entity, like a controller, you can navigate to the related test, view, model, and helper. Place the caret next to edit method, press Ctrl+Alt+Home, select View and press Enter. RubyMine will open the edit.html.erb file containing the corresponding view.

You can use the same shortcut within a view and use the from view to action icon in the editor gutter to quickly go to the corresponding action.

Search everywhere

The next RubyMine feature allows you to search for files, classes, symbols, or options and jump to the entity you need.

Let’s try to find the destroy action within UsersController. Press Shift twice and start typing destroy. The dropdown lists destroy for all controllers within the Symbols group. Select the destroy action from UsersController and press Enter.

Go to symbol

The users_controller.rb file will be opened and the caret will be placed on the definition of the destroy action.

destroy action in editor

Edit code

RubyMine provides multiple code editing features available in the editor that allow you to speed up development process. These include code completion, refactorings, code inspections, and so on.

Code completion

RubyMine can help you complete the names of classes, methods, keywords, and so on. When you invoke code completion, RubyMine analyses the context and suggests the choices applicable to the current caret position.

For instance, open the users_controller.rb file and go to the index method declared in the UsersController class. Type the following code inside the method...

@users = User

... and then type dot. Because the User class is inherited from the ApplicationRecord module, the editor will display all inherited members.

After that, start typing where to filter the list and find the corresponding member from the Querying module, and press Enter.

Intentions

Intentions help you to quickly apply various code changes: convert statements for better code style, add strings to locale dictionaries, use language injections, and so on.

To see intentions in action, open the user.rb file and scroll down to the User.digest method, which uses a multiline ternary operator (?:). According to the Ruby Style Guide, it is preferable to replace such operator with the if/then/else/end block. To do this, place the caret at this ternary expression (for instance, next to ActiveModel) and press Alt+Enter. Press Enter to convert a ternary operator to the if/then/else/end block.

Note that you can check your code and detect possible issues using inspections.

Refactor code

Refactoring is the process of modifying source code in order to make it easier to maintain and extend, but without changing its behavior. Let’s look at some refactoring features available in RubyMine.

Rename refactorings allow you to rename classes, methods, files, variables, and parameters with all the references to them in the code corrected accordingly. Open the users.rb file and scroll down to the downcase_email method raised in the before_save ActiveRecord callback. Place the caret next to this method and press Ctrl+Shift+I to see its definition.

Click Esc and press Ctrl+Shift+Alt+T. Select Rename... in the invoked popup, which suggests various refactorings.

Refactor this popup

In the Rename dialog, specify a new method name (lowercase_email in our case) and click Refactor.

Rename dialog

The Refactor Preview window will display all references to the renamed method.

Do refactor

Click Do Refactor to rename the method in all places.

Extract variable

The Extract Variable refactoring puts the result of the selected expression into a variable. It declares a new variable and uses the expression as an initializer. The original expression is replaced with the new variable.

Open the micropost.rb file and go to the picture_size method. In this method, the picture.size expression is found twice and can be replaced with a variable.

Place the caret at the picture.size expression and press Ctrl+Alt+V. Select this expression in the invoked popup and press Enter. Then, select Replace all 2 occurrences and press Enter again. Finally, specify the variable name and press Enter to finish extracting.

Reformat code

RubyMine allows you to reformat source code to meet the requirements of your code style.

Let's reformat the code of the micropost.rb file. Open this file and press Ctrl+Alt+L.

RubyMine will reformat the entire file and display a number of changed lines.

Analyze code

In this part, we’ll perform static code analysis and detect problems.

RubyMine supports multiple inspection types and, moreover, allows displaying RuboCop offenses inside the IDE. The RuboCop inspection is enabled in RubyMine by default and requires the RuboCop gem to be installed in the project’s SDK. If this gem not installed, RubyMine will suggest doing this.

Install Rubocop

Let’s open the Gemfile containing a list of gems used by the application. Hover the mouse pointer over the warning displayed for the bcrypt gem.

Rubocop warning

RubyMine will display a Rubocop message that notifies you about necessity to order gems in the alphabetical order (see OrderedGems).

Place the caret next to the bcrypt gem and press Alt+Enter. The editor will suggest you fixing all issues related to incorrect gems ordering. Press Enter to do this.

You can also check the entire project and display all warnings in a single report. To do this, select Code | Inspect Code in the main menu. In the invoked dialog, you can specify the desired inspection scope.

Inspection scope

Leave the Whole project option and click OK. The inspection results window will show warnings for a whole project.

Inspection results

You can navigate through this report and fix or suppress specific warnings.

Run tests

RubyMine enables you to use different testing frameworks such as Test::Unit, RSpec or Cucumber.

Run all tests

Our project contains Test::Unit tests in the Tests folder. To run all tests, press Ctrl twice and start typing test. In the invoked popup, select the automatically created test: sample_rails_app run configuration and press Enter.

Run all tests

RubyMine will run and display test results in the Run tool window.

Run specific tests

Now let’s see how to run a specific test. Open the users_controller_test.rb file, scroll down to the should redirect index when not logged in test and click the Run button on the left gutter next to this test.

Run a specific test

In the invoked menu, select Run ‘test_should_redirect...’. RubyMine will display the result for this test.

Test result

Rerun failed tests

Now let’s go back to users_controller_test.rb and break two tests. For the should redirect index when not logged in and should get new tests comment the get users_path and get_sighup_path lines. To do this, select these lines and use the Ctrl+/ shortcut.

Broken tests

Press Ctrl twice and run all tests again. You can see now that these tests failed.

Failed tests

Let’s fix these tests in the currently opened users_controller_test.rb file by uncommenting the get users_path and get_sighup_path lines (Ctrl+/). We can now rerun only these failed tests using the Rerun Failed Tests button.

Now we are ready to run our application.

Run an application

Before running our Rails application, we need to migrate the database. To do this in RubyMine, press Ctrl twice and type db:migrate. Select rake db:migrate in the dropdown and press Enter.

Run Anything / rake db:migrate

Leave the default settings in the invoked Execute ‘db:migrate’ dialog and click OK.

Execute db:migrate

This creates the development.sqlite3 database in the db folder.

Now we can run our application. Press Ctrl twice and start typing development. Select the Development: sample_rails_app configuration from the list and press Enter.

Run Anything / run configuration

RubyMine will show the process of preparing the application to run.

Run tool window / run Rails application

Copy the 0.0.0.0:3000 address used by a web server, insert it to the browser’s address bar and press Enter to see our working application.

Rails application in a browser

Debug an application

One of the key features of RubyMine is debugging support. The debugger provides various ways to examine the state of a running application. You can step through your code and check variable values, set watches on variables to see when values change, and so on.

Set a breakpoint and start debugging

First, open the users_controller.rb file. Set a breakpoint within the create method next to the line where a new user is created.

Set a breakpoint

To start debugging, press Ctrl twice and start typing sample_rails_app. Select the Development: sample_rails_app configuration from the list, hold down the Shift key (the dialog title will be changed to Debug), and press Enter.

Start debugging

If the debase and ruby-debug-ide gems required for debugging have not been installed yet, RubyMine suggests installing them.

Install gems for debugging

After installing the gems, the Debug tool window will show the application output.

Debug console output

Open a browser on the local machine and specify the application address 0.0.0.0:3000.

Rails application in a browser

Click the Sign up now! button. On the Sign up page, enter your credentials and click Create my account.

User credentials

The program will stop when it reaches the breakpoint.

Examine variables

You can now examine the application state and the values of variables.

Stop on a breakpoint

The Frames pane displays the application threads and the corresponding call stacks. In our case, the create method is called in Thread 30. The Variables pane allows you to examine variables available in the current context.

Let’s add the user_params variable to the list. Click the button add in the Variables pane and start typing user_params. Select the user_params variable in the invoked drop-down and press Enter.

Then, click the expand button next to this variable and then expand the @parameters variables in the same way. You can see the user credentials specified in the Sign up form.

Once the breakpoint is hit, we can step through the code.

Step over

Step over proceeds to the next line in the current scope (for example, goes to the next line), without descending into any method calls on the way.

On the screen below, you can see that the User object is not created yet and the @user variable not initialized (equals nil).

Breakpoint

Press F8 or click the Step over button in the Debug window toolbar. The debugger will go to the next line - the if statement. In the editor and Variables pane, you can see that the @user variable was initialized.

Step over

Use the expand button to examine @user properties.

Step into

Step into will cause the debugger to descend into the method calls or blocks on the current line and follow them through. If there are multiple method calls or blocks, you can choose the desired target.

Click the Resume Program button to resume program execution. Go to the browser again and create another user in the Sign up form. The script will stop when it reaches the line where a user is created.

Breakpoint

Press F7 or click the Step into button. The editor will set a focus on the user_params method. You can use arrow keys or Tab to choose the desired method to step into (new or user_params in the example below). Select user_params and press Enter. The program execution will jump to the user_params method definition.

If you press F7 another time, the debugger will suggest you select between the params and require methods from the StrongParameters module.

Debug in console

The debugger has the Console tab that enables you to interact with a debugged application with an IRB-like console.

Start typing user_params in the console, select the corresponding variable and press Enter.

The Console window will display the values of variables.

Last modified: 5 August 2019