Learn RubyMine Tutorials

How to Run Ruby/Rails Commands from the IDE instead of the Terminal

In the Ruby/Rails community, using a terminal is the most popular way to perform various commands and tasks. These tasks include running scripts, tests, Rails generators, Git and Docker commands, and many other things. We also noticed this ourselves when we did some RubyMine usability testing among developers at RailsConf 2019. We found that most participants ran Bundler commands and tests from the IDE terminal.

In this blog post, we’ll discuss the RubyMine features that can be used instead of running specific terminal commands and the benefits that working with the IDE provides. We’ve divided the commands into several groups to allow you to quickly find a corresponding RubyMine feature:

Ruby

ruby script.rb There are multiple ways to run Ruby scripts in RubyMine:

  • Open a script in the editor and press ⌃⇧R / Ctrl+Shift+F10.
  • Right-click a script in the editor or Project view and select Run ‘script’ from the context menu.
  • Press Ctrl twice to invoke the Run Anything popup and execute the ruby script.rb command. In this case, you can specify the required command-line options and script arguments right in the popup, for example, ruby -v script.rb.

All of the above actions create the Ruby run configuration, which stores all the passed parameters. You can customize these parameters, add new ones, and save the configuration to quickly run a script in the future. Learn more from the Run Ruby scripts topic in the documentation.

irb To use the IRB console from inside the IDE, choose one of the following courses of action:

  • From the main menu, select Tools | Run IRB.
  • Press Ctrl twice and execute the irb command in a popup. You can also run a console with additional options, such as passing the -r option to load a script: irb -r ./script. As for running Ruby scripts, additional options will be passed to a newly created IRB run configuration.

You can learn more from the IRB console section of the documentation.

Ruby version managers

rvm use 2.6.3
rvm use 2.6.3@teddy
rvm gemset use teddy
rbenv shell 2.6.3
rbenv local 2.6.3
etc.
Ruby version managers allow you to switch between several versions of Ruby in a terminal. RubyMine detects installed interpreters automatically and enables you to select the desired version in the following ways:

  • Open the Settings/Preferences dialog, go to the Languages & Frameworks | Ruby SDK and Gems page, and select the desired Ruby version and, optionally, a gemset.
  • Press Ctrl twice and execute the required RVM/rbenv command in the Run Anything popup, for example, rvm use 2.6.3@sample_app or rbenv shell 2.6.3.

You can learn more from the Configure a Ruby interpreter section of the documentation.

rvm gemset create teddy To create an RVM gemset, go to the Language & Frameworks | Ruby SDK and Gems page, right-click the required version of Ruby, and select New RVM gemset. You can also create a gemset when you generate a new project.
rbenv gemset create 2.6.3 teddy If you have rbenv-gemset enabled in your project, RubyMine synchronizes the gemsets enabled in the Ruby SDK and Gems page with the ones specified in the .rbenv-gemsets file. So, you can create, enable, and disable rbenv gemsets in two ways:

  • By editing the .rbenv-gemsets file.
  • On the Ruby SDK and Gems page.

You can learn more from the SDK gemsets section of the documentation.

Note that starting from v2020.3, changing the Ruby SDK in RubyMine will automatically change the version of Ruby used in the RubyMine terminal [RUBY-22329].

Bundler

bundle
bundle install
bundle update
etc.
RubyMine integrates with Bundler, so you can install gem dependencies in the following ways:

  • Press Ctrl twice and find the desired bundle command in the Run Anything popup.
  • From the main menu, select Tools | Bundler | Install, or the command you need instead.
  • Open the project’s Gemfile, place the caret at any of the highlighted gems, and press Alt+Enter / ⌥⏎ to install missing gems.

You can find more about integration with Bundler in the Bundler section of the documentation.

bundle exec [command]
(for example, bundle exec rspec spec)
To run a command in the context of the bundle in RubyMine, you need to adjust the settings of the corresponding run configuration (for example, RSpec). In the Run/Debug Configurations dialog, select the required configuration, open the Bundler tab, and enable the Run the script in the context of bundle (bundle exec) option.
bundle gem [gem_name] In RubyMine, you can generate a project skeleton for creating your own Ruby gem. On the Welcome screen, click New Project and select Gem in the Ruby group on the left pane. You can learn more from the Create and publish your first Ruby gem tutorial.

Rake

rake db:create
rake db:migrate
etc.
You can run Rake tasks from the IDE in two ways:

  • From the main menu, select Tools | Run Rake Task or press ⌥R / Ctrl+Alt+R and find the desired task in the Run Anything popup.
  • If you have a *.rake file with your tasks, click the Run Rake Task button on the gutter next to the required task.

As always, you can customize and save parameters for running a Rake task in the created Rake run configuration.

Rails

rails new my_app
rails new my_api --api
etc.
In RubyMine, you can create different types of Rails applications: regular applications, Rails APIs, and mountable engines. On the Welcome Screen, click New Project and select the desired application type from the Rails group on the left pane. Here you can specify the version of Rails, choose the required template, or specify a JavaScript library to be used.
rails g controller
rails g model
rails g scaffold
etc.
RubyMine provides dedicated dialogs for creating Rails elements, such as models, controllers, and so on. There are two ways to generate a Rails entity:

  • From the main menu, select Tools | Run Rails Generator and specify a generator name.
  • Go to File | New and select Rails Generator.

Learn more from Create Rails application elements.
To run a generator with the Spring preloader, ensure that the
corresponding
option is enabled.

rails server To run a Rails application from RubyMine:

  • Press Ctrl twice and execute the rails server command in the popup.
  • Run a required Rails configuration. You can customize the Rails configuration and adjust the server type, IP address and port, environment, etc.
rails console RubyMine has a built-in Rails console so you can interact with your application without leaving the IDE. To open it:

  • Select Tools | Run Rails Console from the main menu.
  • Press Ctrl twice and perform the rails c command in the popup.
rails db:create
rails db:migrate
etc.
To run Rails database migrations in RubyMine, use the corresponding Rake tasks.
rails test
rails test user_test.rb
etc.
Learn how to run Minitest tests in your Rails application in the next section.

Tests

Rake
rake test
rake test:models
rake test:controllers
etc.
Running tests using Rake tasks works the same way as described in the Rake section:

  • From the main menu, select Tools | Run Rake Task or press ⌥R / Ctrl+Alt+R and find the desired task in the Run Anything popup.
  • If you have a *.rake file, click the Run Rake Task button on the gutter next to the required task.
Minitest
ruby -Itest user_test.rb

ruby -Itest user_test.rb -n test_should_be_valid
etc.

RubyMine allows you not only to run specific Minitest tests but also to run all tests in a given directory.

  • Right-click a required directory in the Project view and select Run ‘All tests in …’. You can also use the ⌃⇧R / Ctrl+Shift+F10 shortcut.
  • To run a specific test from the editor, click the gutter icon next to this test. As an alternative, place the caret at this test and press Alt+Enter to run it.

You can learn more about running tests from the IDE from Perform tests section of the documentation.

RSpec
rspec spec

rspec user_spec.rb

rspec user_spec.rb -e "should be valid"

rspec user_spec.rb --only-failures

bundle exec rspec spec
etc.

Running RSpec tests from RubyMine works the same as running Minitests.

  • Right-click a required directory or file in the Project view and select Run ‘All specs in …’. You can also use the ⌃⇧R / Ctrl+Shift+F10 shortcut.
  • To run a specific test from the editor, click the gutter icon next to this test.
  • To rerun failed tests, click the corresponding button on the toolbar.

Note that RubyMine runs tests in the context of the bundle automatically.

spring rspec spec To run tests with the Spring preloader, you need to customize a corresponding run configuration (RSpec, Test::Unit/Shoulda/MiniTest, or Cucumber). In the Run/Debug Configurations dialog, set the Use pre-load server option to Spring.

Rubocop/Standard

rubocop
rubocop app
rubocop --auto-correct
etc.
RubyMine provides two ways to use RuboCop:

  • Enable the RuboCop inspection on the Settings/Preferences | Editor | Inspections page and fix RuboCop offenses right inside the editor.
  • Run the RuboCop inspection from the main menu using the Code | Run inspection by name command (⌥⇧⌘I / Ctrl+Alt+Shift+I). In this case, you can inspect the entire project or a specific range of files.

Learn more from the RuboCop topic.

standardrb
standardrb app
standardrb --fix
etc.
Working with the Standard gem is similar: all you need to do is enable the Standard option on the Settings/Preferences | Editor | Inspections page for the RuboCop inspection.

Capistrano

cap install
cap staging deploy
cap production deploy
etc.
To run Capistrano tasks in RubyMine, do one of the following things:

  • Press Ctrl twice and execute the desired command (for example, cap staging deploy) in the popup.
  • From the main menu, select Tools | Capistrano | Run Capistrano Task and find the desired task.

You can learn more from the Deploy a Rails app using Capistrano tutorial.

Gem commands

RubyMine has a variety of dedicated run configurations for a wide range of Ruby and Rails tools. But there are hundreds of other useful gems and libraries. If you need to execute a command of an arbitrary gem, try to use the Gem Command run configuration. For example, RubyMine does not have a dedicated run configuration for executing Foreman commands. You can start Foreman commands (for example, foreman start) by creating a Gem Command with the required argument. Learn more from Debug an application running with Foreman.

The features we’ve mentioned above don’t come close to capturing all of RubyMine’s capabilities. There are still many useful IDE features that are not related to Ruby/Rails directly, for example:

  • Integration with different Version Control systems allows you to use shortcuts for frequently used commands, such as git checkout, git pull, git merge, etc.
  • Docker support allows you to manage images, containers, and services in a dedicated tool window instead of executing docker pull, docker-compose up, and other commands in a terminal.

That’s about it for now. As always, you can use our issue tracker, Twitter, or the comments section below to get in touch with us and let us know if you need help with the IDE or if you would like to suggest any new features.

Cheers,
Your RubyMine team

image description