Last Updated: February 25, 2016
·
1.143K
· zedtux

Use Rubocop in any Ruby project without installing it

One of the applications we are developing at KNEIP is not yet migrated to Ruby 1.9 but we would like to use Rubocop anyway.

Rubocop is Ruby 1.9 only and thanks to RVM or rbenv we can use different Rubies so I created the following alias in order to be able to use in any ruby projects (no matter which ruby version is required) without installing it as a development dependency:

alias cop='rvm 2.1.0 do rubocop'
alias rcop='rvm 2.1.0 do rubocop -R'

Those aliases are using RVM and request that the installed ruby 2.1.0 execute the rubocop command with argument -R for Rails so that now I can do the following:

$ cd ~/dev/project-1/
$ cop lib/my_class.rb
Inspecting 1 file
.

1 file inspected, no offences detected

Or

$ cd ~/dev/project-2/
$ cop
Inspecting 29 files
WCCCCCCCCCCW.CCCCWWWCWWCWW.CC

Offences:
...
29 files inspected, 1046 offences detected

And the same with rcop for a Rails project.