Believe what you want, it doesn’t mean you’re right
I am attempting to make the idea of shared code ownership more popular at $work, and one of the “irritants” is the variety of formatting people manage to do — for example the use of hard tabs with the assumption that tab stops are two columns apart … fine if we all have our editors set up the same way, but the bulk of external tools assume eight column tab spacing and the code suddenly becomes very wide outside the editor. Rubocop was an excuse for me to use “community standards”, and to try and find a way to enforce them.
I managed to integrate rubocop and syntastic into the workflow I use on a Ruby 1.8.7 code base at $work, and I found it the inclusion of syntastic in my vim setup to make a huge difference to the way I worked.
Syntastic provides almost immediate feedback when I save a file, so if I’m working in a “clean” file and syntastic creates a gutter on the left of the screen then I know that I need to look at something. This automated feedback helps me keep things tidy as I write, much like guard helps me keep my tests passing by running them as files are updated.
I have used rubocop for ages at home, and it has been easy to integrate because I am using supported Ruby versions — that is more recent than 1.8.7 — with chruby to switch, so there was always a rubocop lurking on my path as long as I remembered to use it. Rubocop is a essentially a creature of Ruby 1.9+
So I had three main things to sort out: a Ruby 1.8.7-ish .rubocop.yml file, make sure that rubocop runs even if my .ruby-version file specifies a 1.8.7 version, and helping my co-workers get into the habit of making use of syntastic.
The basis I use for a 1.8.7-ish rubocop.yml file is this:
# Ruby 1.8.7 needs the . on a chain of method calls at the end of a line.
# Ruby 1.8.7 doesn't have the -> lambda
# Ruby 1.8.7 doesn't have 1.9 style hash syntax
# This is my personal preference...
To make rubocop work in my environment when I was in a directory with a .ruby-version file I created the following executable rubocop in my bin directory. It assumes I have installed ruby 2.1.2 and the rubocop gem for ruby 2.1.2, and it works with chruby; if you’re using rbenv or rvm you’ll need to invent your own.
# Wrapper for rubocop so that I can use it on projects which use older rubies
# for which there is no rubocop available as a gem.
# chruby puts its gem & ruby bins at the beginning of the path, so the real
# rubocop ought to be found first. The reason for having the full path is to
# avoid ~/bin/rubocop calling itself if I have forgotten to install the rubocop
# gem for the specified ruby gem.
exec "$HOME/.gem/ruby/$ruby_version/bin/rubocop" "$@"
Finally to get syntastic integrated with vim, using vundle, I suggested adding these lines to our .vimrc files:
let g:syntastic_ruby_checkers = ['rubocop']
… and then we were all set.