Integrate Rubocop in your workflow

Rubocop is a tool that helps you to check and enforce your style guide for your
Ruby code. It does this using static analysis. The Github page says:

A Ruby static code analyzer, based on the community Ruby style guide.

It uses the Community style guide as a default.

We will talk about the following:

  • Installing Rubocop
  • Configure Rubocop to be your style guide
  • Create a todo list of violations
  • Rubocop in your editor
  • Run Rubocop as part of your test suite

Installing Rubocop

Installing Rubocop is as easy as any gem to install. Just pop the following line
in your gemfile:

gem 'rubocop', require: false  

And run $ bundle install.

Now that we have installed Rubocop to our app, we can run it to see what
violations we have: $ rubocop.

When you run it for the first time, there is a big chance you have some
violations. The output will look as follow:

[jeroen] ~/Development/projectname [master] $ rubocop
Inspecting 19 files  
CCCCCCCCCCCCCCCCCCC

Offenses:

config.rb:5:13: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.  
Time.zone = "Amsterdam"  
            ^^^^^^^^^^^
config.rb:10:17: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.  
  blog.prefix = "blog"
                ^^^^^^
config.rb:12:20: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.  
  blog.permalink = "{title}.html"
                   ^^^^^^^^^^^^^^
config.rb:13:31: C: Prefer single-quoted strings when you don't need string interpolation or special symbols.  
  blog.new_article_template = "source/blog/template.html.erb"
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
...

You can now use the output of Rubocop to start fixing the issues that you think
are important to fix. But chances are, the community style guide does not 100%
match your own style guide. Maybe you prefer double-quotes? Luckily you can
modify Rubocop to match your own guide!

Configure Rubocop to be your style guide

When you create a .rubocop.yml file in the root of your project, Rubocop will
use this settings file by default when you run $ rubocop.

But the file is empty? You might wonder: where should I start? Luckily the
people behind Rubocop have added the default information to the Github repo:
https://github.com/bbatsov/rubocop/blob/master/config/default.yml.

So if you want to change the single-quote rule to double-quotes, you add the
following to your .rubocop.yml file:

Style/StringLiterals:  
  EnforcedStyle: double_quotes
  Enabled: true

If you run $ rubocop again, it will stop complaining about the wrong usage of
quotes! There are a lot more options you can set, I advice you to go over the
defaults.yml file and see what works for you and what not.

Create a todo list of violations

When you first add Rubocop to your project it might look overwhelming to see all
the violations. Where to start? How would I ever fix all those errors? What if I
told you Rubocop can generate a "todo list" for you of all the violations? So
you can start fixing them one by one in your own pace?

You can run Rubocop with the following command: $ rubocop --auto-gen-config.
This will generate a .rubocop_todo.yml file. In order to have it loaded in
your normal Rubocop config you need to add the following to the top of the
.rubocop.yml file:

inherit_from: .rubocop_todo.yml  

If you run Rubocop now, you see that all the checks are passing. This is because
the rubocop_todo.yml has disabled all the checks that cause a violation. By
doing so you are sure that no new violations get introduced, and the ones that
are there, you can fix when you have a spare moment.

When we look in the .rubocop_todo.yml file we see something like this:

# This configuration was generated by `rubocop --auto-gen-config`
# on 2015-05-04 12:53:40 +0200 using RuboCop version 0.28.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 17
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
Style/AlignParameters:  
  Enabled: false

# Offense count: 1
# Cop supports --auto-correct.
Style/EmptyLines:  
  Enabled: false

# Offense count: 1
# Configuration parameters: Exclude.
Style/FileName:  
  Enabled: false

# Offense count: 4
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
Style/HashSyntax:  
  Enabled: false

# Offense count: 45
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, SupportedStyles.
Style/StringLiterals:  
  Enabled: false

Changing an entry from Enabled: false to Enabled: true will make Rubocop
check for that violation again. So when when we want to fix the
Style/EmptyLines we do the following:

  • Enable the Style/EmptyLines check
  • Run Rubocop
  • Fix the violations
  • Run Rubocop again (all should be good now)
  • Remove the entire Style/EmptyLines entry from the .rubocop_todo.yml

Repeat this process a couple of times until the .rubocop_todo.yml is empty.
Trust me, its worth it!

Rubocop in your editor

The great thing about Rubocop is, it has great plugins for most editors! Here
is a small list of the most used editors and the plugins that you can download
to have Rubocop integration:

Run Rubocop as part of your test suite

Now that you have set-up Rubocop to your likings and added it to you favourite
editor, the next step is to add it to your CI pipeline! We use Codeship for our
continues Integration, and setting it up is as easy as adding another bash
command to our Test Commands.

Whenever we push a new commit, the test will automatically run, and so will
Rubocop. If there is a violation, our test suite will now fail and tell the
committer that there is something wrong! Awesome!

Questions?

If you have any questions about how we set this up in our projects, please ask
me on Twitter (@jvanbaarsen), or email us at
hello@firmhouse.com.