Basic Usage
RuboCop has three primary uses:
-
Code style checker (a.k.a. linter)
-
A replacement for
ruby -w
(a subset of its linting capabilities) -
Code formatter
In the next sections we’ll briefly cover all of them.
Code style checker
Running rubocop
with no arguments will check all Ruby source files
in the current directory:
$ rubocop
Alternatively you can pass rubocop
a list of files and directories to check:
$ rubocop app spec lib/something.rb
Here’s RuboCop in action. Consider the following Ruby source code:
def badName
if something
test
end
end
Running RuboCop on it (assuming it’s in a file named test.rb
) would produce the following report:
Inspecting 1 file W Offenses: test.rb:1:1: C: Style/FrozenStringLiteralComment: Missing magic comment # frozen_string_literal: true. def badName ^ test.rb:1:5: C: Naming/MethodName: Use snake_case for method names. def badName ^^^^^^^ test.rb:2:3: C: Style/GuardClause: Use a guard clause instead of wrapping the code inside a conditional expression. if something ^^ test.rb:2:3: C: Style/IfUnlessModifier: Favor modifier if usage when having a single-line body. Another good alternative is the usage of control flow &&/||. if something ^^ test.rb:4:5: W: Layout/EndAlignment: end at 4, 4 is not aligned with if at 2, 2. end ^^^ 1 file inspected, 5 offenses detected
Autocorrecting offenses
You can also run RuboCop in an autocorrect mode, where it will try to automatically fix the problems it found in your code:
$ rubocop -a
# or
$ rubocop --autocorrect
See Autocorrect for more details. |
Changing what RuboCop considers to be offenses
RuboCop comes with a preconfigured set of rules for each of its cops, based on the Ruby Style Guide. Depending on your project, you may wish to reconfigure a cop, tell to ignore certain files, or disable it altogether.
The most common way to change RuboCop’s behavior is to create a configuration file named .rubocop.yml
in the
project’s root directory.
For more information, see Configuration.
RuboCop as a replacement for ruby -w
RuboCop natively implements almost all ruby -w
lint warning checks, and then some. If you want you can use RuboCop
simply as a replacement for ruby -w
:
$ rubocop -l
# or
$ rubocop --lint
RuboCop as a formatter
There’s a handy shortcut to run autocorrection only on code layout (a.k.a. formatting) offenses:
$ rubocop -x
# or
$ rubocop --fix-layout
This option was introduced in RuboCop 0.57.0. |
Command-line flags
For more details check the available command-line options:
$ rubocop -h
To specify multiple cops for one flag, separate cops with commas and no spaces:
$ rubocop --only Rails/Blank,Layout/HeredocIndentation,Naming/FileName
Command flag | Description |
---|---|
|
Autocorrect offenses (only when it’s safe). See Autocorrect. |
|
Deprecated alias of |
|
Autocorrect offenses (safe and unsafe). See Autocorrect. |
|
Deprecated alias of |
|
Generate a configuration file acting as a TODO list. |
|
Force color output on or off. |
|
Run with specified config file. |
|
Store and reuse results for faster operation. |
|
Displays some extra debug output. |
|
Run without pending cops. |
|
Used with --autocorrect to annotate any offenses that do not support autocorrect with |
|
Displays cop names in offense messages. Default is true. |
|
Display elapsed time in seconds. |
|
Only output offense messages at the specified |
|
Only output correctable offense messages. |
|
Only output safe correctable offense messages. |
|
Run with pending cops. |
|
Run all cops enabled by configuration except the specified cop(s) and/or departments. |
|
Limit how many individual files |
|
Displays extra details in offense messages. |
|
Choose a formatter, see Formatters. |
|
Inspect files in order of modification time and stops after first file with offenses. |
|
Minimum severity for exit with error code. Full severity name or upper case initial can be given. Normally, autocorrected offenses are ignored. Use |
|
Force excluding files specified in the configuration |
|
Inspect files given on the command line only if they are listed in |
|
Print usage information. |
|
Ignores all Exclude: settings from all .rubocop.yml files present in parent folders. This is useful when you are importing submodules when you want to test them without being affected by the parent module’s rubocop settings. |
|
Ignore unrecognized cops or departments in the config. |
|
Generate a .rubocop.yml file in the current directory. |
|
Run only lint cops. |
|
List all files RuboCop will inspect. |
|
Generate only |
|
Include the date and time when |
|
Show offense counts in config file generated by |
|
Run only the specified cop(s) and/or cops in the specified departments. |
|
Write output to a file instead of STDOUT. |
|
Use available CPUs to execute inspection in parallel. Default is parallel. |
|
Raise cop-related errors with cause and location. This is used to prevent cops from failing silently. Default is false. |
|
Require Ruby file (see Loading Extensions). |
|
Regenerate the TODO list using the same options as the last time it was generated with |
|
Run only safe cops. |
|
Deprecated alias of |
|
Shows available cops and their configuration. |
|
Shows urls for documentation pages of supplied cops. |
|
Write all output to stderr except for the autocorrected source. This is especially useful when combined with |
|
Pipe source from STDIN. This is useful for editor integration. Takes one argument, a path, relative to the root of the project. RuboCop will use this path to determine which cops are enabled (via eg. Include/Exclude), and so that certain cops like Naming/FileName can be checked. |
|
Optimize real-time feedback in editors, adjusting behaviors for editing experience. Editors that run RuboCop directly (e.g., by shelling out) encounter the same issues as with |
|
Display style guide URLs in offense messages. |
|
Autocorrect only code layout (formatting) offenses. |
|
Displays the current version and exits. |
|
Displays the current version plus the version of Parser and Ruby. |
Default command-line options are loaded from .rubocop
and RUBOCOP_OPTS
and are combined with command-line options that are explicitly passed to rubocop
.
Thus, the options have the following order of precedence (from highest to lowest):
-
Explicit command-line options
-
Options from
RUBOCOP_OPTS
environment variable -
Options from
.rubocop
file.
Exit codes
RuboCop exits with the following status codes:
-
0
if no offenses are found or if the severity of all offenses are less than--fail-level
. (By default, if you use--autocorrect
, offenses which are autocorrected do not cause RuboCop to fail.) -
1
if one or more offenses equal or greater to--fail-level
are found. (By default, this is any offense which is not autocorrected.) -
2
if RuboCop terminates abnormally due to invalid configuration, invalid CLI options, or an internal error.
Parallel Processing
RuboCop enables the --parallel
option by default. This option allows for
parallel processing using the number of available CPUs based on Etc.nprocessors
.
If the default number of parallel processes causes excessive consumption of CPU and memory,
you can adjust the number of parallel processes using the $PARALLEL_PROCESSOR_COUNT
environment variable.
Here is how to set the parallel processing to use two CPUs:
$ PARALLEL_PROCESSOR_COUNT=2 bundle exec rubocop
Alternatively, specifying --no-parallel
will disable parallel processing.