This site is now 100% read-only, and retired.

Using the rubocop ruby code and style analyzer

Posted by Steve on Mon 9 Mar 2015 at 12:12

If you're developing code that you expect others to read and work with then it is very important to pick a consistent style, and having a style-checker can make enforcing your choice much less painful. Here we'll take a look at the rubocop analyzer for Ruby.

Installing Rubocop

If you're a Ruby-user you're presumably already familiar with installing gems, but if not it isn't so difficult. The biggest choice is whether you install the gems beneath a particular prefix, or whether you do so globally.

If you're root you can install the tool globally via:

# gem install rubocop

This will give you /usr/local/bin/rubocop, as the binary.

If you prefer you can install beneath you home directory, with no special permissions:

$ export GEM_HOME=~/gems/
$ gem install rubocop
Fetching: rainbow-2.0.0.gem (100%)
Successfully installed rainbow-2.0.0
Fetching: ast-2.0.0.gem (100%)
Successfully installed ast-2.0.0
Fetching: parser-2.2.0.3.gem (100%)
Successfully installed parser-2.2.0.3
...
7 gems installed

If you go this route you will want to remember add your new directory to your PATH:

$ export PATH=$PATH:$HOME/gems/bin/

Using Rubocop

The simplest way to run rubocop is to invoke it, with no arguments, inside a directory containing Ruby source code. When started it will recursively look for source code, and report on the style and code warnings it finds.

However by default the warnings will most likely be numerous. So your first run should actually be to scan your code, identify all the warnings, then disable them by generating a configuration file.

To do this find your source tree and run the following two commands:

~/hg/custodian $ rubocop --auto-gen-config
~/hg/custodian $ mv .rubocop_todo.yml .rubocop.yml

The configuration file, .rubocop.yml, is read from the current directory and allows individual style and formatting checks to be disabled. If you look at it you'll find content like this:

$ tail .rubocop.yml
# Offense count: 2
# Configuration parameters: EnforcedStyle, SupportedStyles.
Style/VariableName:
  Enabled: false

# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: WordRegex.
Style/WordArray:
  MinSize: 2

Here you see a list of things such as "Style/VariableName" which is a particular offence, which occurred twice, and which is now disabled ("Enabled: false").

Now you have this configuration file you can start removing things from it, fixing up your code as you go.

Quick Testing

Rather than allowing the tool to run recursively the alternative is to name files explicitly:

$ rubocop lib/custodian/protocoltest/ping.rb
...
lib/custodian/protocoltest/ping.rb:203:26:
C: Prefer single-quoted strings when you don't need string interpolation or special symbols.
      register_test_type "ping"
                         ^^^^^^

This is another way to update your codebase, rather than using the configuration file and enabling tests one by one you could fix your codebase one file at a time.