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?