Role models are important.
-- Officer Alex J. Murphy / RoboCop
RuboCop is a Ruby code style checker based on the Ruby Style Guide.
RuboCop's installation is pretty standard:
$ gem install rubocop
rubocop with no arguments will check all Ruby source files
in the current folder:
Alternatively you can pass
rubocop a list of files and folders to check:
$ rubocop app spec lib/something.rb
For more details check the available command-line options:
$ rubocop -h
||Displays the current version and exits|
||Displays some extra debug output|
||Output the results in Emacs format|
||Run with specified config file|
||Suppress the final summary|
||Run only the specified cop|
The behavior of RuboCop can be controlled via the .rubocop.yml configuration file. The file can be placed either in your home folder or in some project folder.
RuboCop will start looking for the configuration file in the directory where the inspected file is and continue its way up to the root folder.
The file has the following format:
inherit_from: ../.rubocop.yml Encoding: Enabled: true LineLength: Enabled: true Max: 79
It allows to enable/disable certain cops (checks) and to alter their behavior if they accept any parameters.
inherit_from directive is used to include configuration
from one or more files. This makes it possible to have the common
project settings in the
.rubocop.yml file at the project root, and
then only the deviations from those rules in the subdirectories. The
included files can be given with absolute paths or paths relative to
the file where they are referenced. The settings after an
inherit_from directive override any settings in the included
file(s). When multiple files are included, the first file in the list
has the lowest precedence and the last one has the highest. The format
for multiple inclusion is:
inherit_from: - ../.rubocop.yml - ../conf/.rubocop.yml
config/default.yml under the RuboCop home directory
contains the default settings that all configurations inherit
from. Project and personal
.rubocop.yml files need only make
settings that are different from the default ones. If there is no
.rubocop.yml file in the project or home direcotry,
config/default.yml will be used.
Disabling Cops within Source Code
One or more individual cops can be disabled locally in a section of a file by adding a comment such as
# rubocop:disable LineLength, StringLiterals [...] # rubocop:enable LineLength, StringLiterals
You can also disable all cops with
# rubocop:disable all [...] # rubocop:enable all
One or more cops can be disabled on a single line with an end-of-line comment.
for x in (0..19) # rubocop:disable AvoidFor
RuboCop checks all files recursively within the directory it is run
on. However, it does not recognize some files as Ruby(only files
.rb or extensionless files with a
are automatically detected) files, and if you'd like it to check these
you'll need to manually pass them in. Files and directories can be
also be ignored through
Here is an example that might be used for a Rails project:
AllCops: Includes: - Rakefile - config.ru Excludes: - db/** - config/** - script/** # other configuration # ...
Note: Files and directories are specified relative to the
Unfortunately every major Ruby implementation has its own code analysis tooling, which makes the development of a portable code analyzer a daunting task.
RuboCop currently supports MRI 1.9 and MRI 2.0. Support for JRuby and Rubinius is not planned at this point.
rubocop.el is a simple Emacs interface for RuboCop. It allows you to run RuboCop inside Emacs and quickly jump between problems in your code.
flycheck > 0.9 also supports RuboCop and uses it by default when available.
The vim-rubocop plugin runs RuboCop and displays the results in Vim.
There's also a RuboCop checker in syntastic.
Sublime Text 2
If you're a ST2 user you might find the Sublime RuboCop plugin useful.
Here's one great opportunity to contribute to RuboCop - implement RuboCop integration for your favorite editor.
Here's a list of all the people who have contributed to the development of RuboCop.
I'm extremely grateful to each and every one of them!
I'd like to single out Jonas Arvidsson for his many excellent code contributions as well as valuable feedback and ideas!
If you'd like to contribute to RuboCop, please take the time to go through our short contribution guidelines.
Converting more of the Ruby Style Guide into RuboCop cops is our top priority right now. Writing a new cop is a great way to dive into RuboCop!
Of course, bug reports and suggestions for improvements are always welcome. GitHub pull requests are even better! :-)
If you're interested in everything regarding RuboCop's development, consider joining its Google Group.
RuboCop's changelog is available here.
Copyright (c) 2012-2013 Bozhidar Batsov. See LICENSE.txt for further details.