RubyMine 2017.2 Help

Creating and Running Your First Ruby Project

Before you start

Make sure that the following prerequisites are met:

Choosing interpreter

As we are using macOS and RVM, let's create a specific gemset for our project. To do that, just click Create link next to the words Use gemset 'QuadraticEquation'.

Creating a simple Ruby script in RubyMine

Do you remember the quadratic formula from math class? This formula is also known as the A, B, C formula, it’s used for solving a simple quadratic equation: ax2 + bx + c = 0. As manually solving quadratic formulas gets boring quickly, let’s replace it with a script.

Let’s start our project: if you’re on the Welcome screen, click Create New Project. If you’ve already got a project open, choose File | New Project.

RubyMine suggests several project templates for the creation of the various types of applications (Rails, Puppet, etc.). When RubyMine creates a new project from a project template, it produces the corresponding directory structure and specific files, and any needed run configurations or settings.

In this tutorial we’ll create a simple Ruby script, so we’ll choose Empty Project. This template will create an empty project for us.

Choose the project location. To do that, click /help/img/idea/2017.2/browseButton.png button next to the Location field, and specify the directory for your project.

Then click the Create button at the bottom of the New Project dialog.

/help/img/idea/2017.2/rm_create_ruby_project.png

If you’ve already got a project open, after clicking Create RubyMine will ask you whether to open a new project in the current window or in a new one. Choose Open in current window - this will close the current project, but you'll be able to reopen it later. See the page Opening Multiple Projects for details.

Creating a Ruby file

Select the project root in the Project tool window, and press Alt+Insert. Choose File, and then type the name of the new file:

/help/img/idea/2017.2/rm_create_ruby_file.png

RubyMine creates a new Ruby file and opens it for editing.

/help/img/idea/2017.2/rm_create_ruby_file_open.png

Editing source code

Let's first have a look at the Ruby file we've just generated.

Immediately as you start typing, you should see that RubyMine, like a pair-programmer, looks over your shoulder and suggests how to complete your line. For example, you want to create a print statement. As you just start typing the keyword, a suggestion list appears:

/help/img/idea/2017.2/rm_create_ruby_file_completion.png

Note the stripes in the right gutter. Hover your mouse pointer over a stripe, and RubyMine shows a balloon with the detailed explanation.

/help/img/idea/2017.2/rm_right_gutter_message.png

Since RubyMine analyses your code on-the-fly, the results are immediately shown in the inspection indicator on top of the right gutter. This inspection indication works like a traffic light: when it is green, everything is OK, and you can go on with your code; a yellow light means some minor problems that however will not affect compilation; but when the light is red, it means that you have some serious errors.

For the example, let's use this code: (you can either type it yourself, or use the copy button in the top right of the code block here in the help):

p "insert first quotient" a = gets.to_f p "insert second quotient" b = gets.to_f p "insert third quotient" c = gets.to_f discriminant = b**2 - 4 * a * c square_root = Math.sqrt(discriminant) x1 = ((-b + square_root) / (2 * a)) x2 = ((-b - square_root) / (2 * a)) p "D = #{discriminant}, x1 = #{x1}, x2 = #{x2}"

Running your application

Let’s try out our code by calculating the roots of x2 + 10x + 2. To do that, right-click the editor, and on the context menu choose to run the script (Ctrl+Shift+F10). Then, in the console, enter a = 1, b = 10, and c = 2, and see the result:

/help/img/idea/2017.2/rm_run_result.png

See the Running section for more details about configuring how your code is executed by RubyMine.

Run/debug configuration

Let's make a brief excursion into an important notion of a run/debug configuration.

Each script is executed using a special profile, or a run/debug configuration. Such a profile is used for running, debugging and testing applications, and specifies the script name, working directory, actions to be performed before launch, etc.

RubyMine suggests a number of default run/debug configurations for the various types of applications (Ruby scripts, Rails applications, tests, etc.) You can view the available defaults in the Run/Debug Configurations dialog, which is invoked in numerous ways, for example, by Run | Edit Configurations... commands on the main menu, or by clicking the drop-down list in the Run area of the main toolbar.

When we run the script just now, RubyMine created a temporary run/debug configuration for us. Let’s first save this configuration: go to the run configuration dropdown on the top-right of the editor, and choose Save configuration.

/help/img/idea/2017.2/rm_save_run_config.png

Afterwards, choose Edit Configurations to have a look at what is happening here.

/help/img/idea/2017.2/rm_run_config_dialog.png

The lower node contains the list of default run/debug configurations. These default run/debug configurations are nameless, but each new run/debug configuration is created on the grounds of a default one, and gets the name of your choice.

The upper node is called Ruby and contains just one run/debug configuration QuadraticEquation, which is shown grayed out. What does it mean?

Run/debug configuration QuadraticEquation is a temporary profile, which RubyMine has produced, when you've just run the QuadraticEquation script. It resides under the node Ruby, since this run/debug configuration is created on the base of the default configuration of the Ruby type.

You can save this run/debug configuration and thus make it permanent. Permanent run/debug configurations are rendered in a normal font. Unlike temporary configurations, the number of permanent ones is unlimited.

If you’d like to change how your program is executed by RubyMine, this is where you can configure various settings like: command-line parameters, work directory, and more. See Run/Debug configurations for more details.

If you’d like to start the script using this Run configuration, use the /help/img/idea/2017.2/run.png button next to the dropdown.

Summary

Congratulations on completing your first script in RubyMine! Let's repeat what you've done with the help of RubyMine:

  • Created a project.
  • Created a file in the project.
  • Created the source code.
  • Ran this source code.
  • Saved the run/debug configuration.

In the next step, learn how to debug your programs in RubyMine.

Last modified: 26 October 2017

See Also