Miskatonic University Press

Rubocop

code emacs

I hack a fair bit of Ruby, usually in a pretty plain way to solve a text-based problem; I can write scripts to do what I need, because my problems aren’t too large, but what I write isn’t elegant. Just recently I started using Rubocop in Emacs, and wow, what a great tool! It’s making the code better and me a better coder.

Rubocop is written by Bozhidar Batsov, who also made rubocop-emacs to integrate it into Emacs. I’ve had that running for a while and found it useful for pointing out unused variables and things like that, but otherwise it didn’t seem to do a lot. That’s because I had it turned on but wasn’t actually using it.

Here’s a short Ruby script:

#!/usr/bin/env ruby

puts 'This is an example.'

x = 5

y = ["foo", "bar", "baz"]

if 2 > 1
  puts "Yes, 2 > 1"
else
end

What it looks like in my Emacs with rubocop-mode on:

Before.
Before.

The lines in red are highlighted because there’s a problem: in this case, the variables aren’t reused so, as the script stands, they could be removed without affecting anything.

Now, if I run rubocop on this script at the command line, it tells me helpful things about making the script better. If I run M-x rubocop-check-current-file in Emacs it tells me the same helpful things. But if I run M-x rubocop-autocorrect-current-file it fixes the problems.

Here’s the output:

-*- mode: compilation; default-directory: "~/" -*-
Compilation started at Thu Oct 13 20:28:30

rubocop -a --format emacs /home/wtd/rubocop.rb
/home/wtd/rubocop.rb:3:6: C: [Corrected] Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
/home/wtd/rubocop.rb:5:1: W: Useless assignment to variable - `x`.
/home/wtd/rubocop.rb:7:1: W: Useless assignment to variable - `y`.
/home/wtd/rubocop.rb:7:5: C: [Corrected] Use `%w` or `%W` for an array of words.
/home/wtd/rubocop.rb:9:1: C: [Corrected] Favor modifier `if` usage when having a single-line body. Another good alternative is the usage of control flow `&&`/`||`.
/home/wtd/rubocop.rb:11:1: C: [Corrected] Redundant `else`-clause.

Compilation exited abnormally with code 1 at Thu Oct 13 20:28:30
During.
During.

(I can hit Enter on any of those lines and jump to that line in the code.)

All of the corrections are corrected and the warnings are left alone. The code is changed to this:

#!/usr/bin/env ruby

puts "This is an example."

x = 5

y = %w(foo bar baz)

puts "Yes, 2 > 1" if 2 > 1
After.
After.

Magic! It’s up to me to decide what to do with x and y, but all the style problems are fixed. That’s just a tiny example. Rubocop does a lot more, and I’ve been going through scripts changing CGI::parse to CGI.parse or foo["bar"] to foo[:bar] and all sorts of other small things that make them more readable and consistent.

The one thing I didn’t like about Rubocop was its preference for ‘single’ quotes over “double” quotes, so I put this in a new ~/.rubocop.yml file to tell it how I like things:

Style/StringLiterals:
  EnforcedStyle: double_quotes

Bozhidar Batsov: thank you.