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.
Jeroen van Baarsen
May 4, 2015

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:

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:

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:

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:

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:

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.

codeship

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.

Receive updates
Receive our latest product updates and blog posts in your inbox. No spam, just the latest about the Firmhouse platform.
Read about our privacy policy.
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.