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.
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.
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
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
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"
}
This is what RoboCop looks like 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?