logo

Sue Feng Design

ā€¹ Back to blog

Rubocop

I recently found out about a really handy tool called rubocop thatā€™s great for developing in Ruby. You can use it with SublimeText, VSCode, and probably other text editors out there as well. Today Iā€™ll be showing you about setting up rubocop with SublimeText in Mac OS.

What is rubocop?

Rubocop uses style guides from the Ruby Style Guides community to analyze code you write, and it tells you if any of your code breaks the rules of the style guides.

rubocop logo

Install and run rubocop

Make sure you have a ruby environment on your computer. Then run the following command in your command line:

$ gem install rubocop

To use it straight out of the box, just type this in your command line:

$ cd my/cool/ruby/project
$ rubocop

Then rubocop should print a list of errors and descriptions of those errors for you.

rubocop in command line

Example error:

validation.rb:14:3: C: Metrics/PerceivedComplexity: Perceived complexity for single_value is too high. [8/7]
  def single_value(restrictions, chosen) ...
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Customizing rubocop

If you want to customize it, like say you donā€™t want the Ruby version rubocop uses to be 1.9 since the version you use is 2.3, then you can edit it in the config file.

$locate rubocop should list some locations for where it is. Or if you already have an idea of where itā€™s located, say you are using rbenv, then youā€™ll want to navigate to it here:

~/.rbenv/versions/<version#>/lib/ruby/gems/<version#>/gems/rubocop<version#>/config/default.yml

Where <version#> is, thatā€™s the version number thatā€™s there.

Open default.yml. Then find where it says TargetRubyVersion: ~ and edit the ~ to 2.3.

Then find where it says Style/HashSyntax and edit where it says EnforcedStyle: ruby19 and change it to EnforcedStyle: no_mixed_keys.

Something you donā€™t understand?

If thereā€™s anything you donā€™t understand with the terminology or with customizing rubocop, they have documentation you can search through. You may also check the Ruby Style Guides for more information.

Setting up rubocop to work in SublimeText

If you donā€™t already have Package Control, get it, as it will make your life easier for installing packages. Hereā€™s an article I wrote a while back on SublimeText, and thereā€™s a section on Package Control there.

If you have Package Control, youā€™ll want to get this package first if you donā€™t already have it: SublimeLinter.

Then get SublimeĆ¢ā‚¬ā€¹Linter-rubocop.

Youā€™ll need to set up some paths before you can use it. Navigate to the SublimeLinter settings:

SublimeText -> Preferences -> Package Settings -> SublimeLinter -> Settings

SublimeLinter settings

Under Linters, youā€™ll have something like this:

"linters": {
    "php": {
        "@disable": false,
        "args": [],
        "excludes": []
    },
    "rubocop": {
        "@disable": false,
        "args": [],
        "excludes": []
    },
    "ruby": {
        "@disable": false,
        "args": [],
        "excludes": []
    }
},

If you donā€™t have the PHP or Ruby linters, thatā€™s fine. Itā€™s the rubocop one we care about here. Add this between the brackets for "args", so it looks like this:.

"rubocop": {
    "@disable": false,
    "args": ["--config",
        "/Users/<user>/.rbenv/versions/<version#>/lib/ruby/gems/<version#>/gems/rubocop-<version#>/config/default.yml"],
    "excludes": []
},

Youā€™ll want to put your own , and <version#> values that work for your setup.

For a more detailed copiable output and inline error messages, you may get RuboĆ¢ā‚¬ā€¹Cop.

Youā€™ll have to edit the settings for that as well. Navigate to this location:

SublimeText -> Preferences -> Package Settings -> RoboCop -> Settings ā€” Default

SublimeText -> Preferences -> Package Settings -> RoboCop -> Settings ā€” User

Open the Default settings first, and then the User settings, and you can put in the user settings you want. These are the ones I set:

{
  "check_for_rbenv": true,
  "rbenv_path": "/usr/local/bin/rbenv",
  "rubocop_config_file": "/Users/<user>/.rbenv/versions/<version#>/lib/ruby/gems/<version#>/gems/rubocop-<version#>/config/default.yml"
}

RuboCop settings

This is what RoboCop looks like in action:

RoboCop in action

Your turn

Do you use linters and/or style guides when coding? If so, what do you use?

Use a different text editor? What or which ones do you use?

Posted on: January 31, 2018Categories: TutorialsTags: coding
ā€¹ Back to blog