RuboCop can be a convenient static code analysis tool to apply the Ruby style guide to your apps. Sometimes the rules it enforces are not suitable for some of your files and you need to disable or customise the checks RuboCop uses.
In this quick tutorial I will show you some options to enable and disable RuboCop code checks, called “cops”, so that it works effectively for your application.
- Disable and enable cops within a file
- Configure your .rubocop.yml
- Useful RuboCop CLI commands
- Find out more
In RuboCop lingo the various checks performed on the code are called cops. Each cop is responsible for detecting one particular offense. There are several cop departments, grouping the cops by class of offense. — RuboCop docs: Cops
Disable and enable cops within a file
You can disable a code check within a file, for example a function or section of your code, through annotating it with comments RuboCop can read.
These start with # rubocop:disable followed by the the checks you want to disable. You can specify one cop or many separated by a comma.
# rubocop:disable Metrics/LineLength, Style/StringLiterals, ...
Disable cops on a single line
If you add the RuboCop comment at the end of a line it will disable it for just that aspect, such as a method, block or class.
def very_long_method # rubocop:disable Metrics/MethodLength ... end
This choice is best if you just want to change how one aspect in a file is affected by RuboCop without needing to think about reenabling the checks again.
Disable cops until you reenable them
Another option is to disable checks before the start of a section of code, and then enable them again at a point you specify. Similar the RuboCop comment to disable cops, the comment to activate checks again is # rubocop:enable followed by the name of the relevant cops.
# rubocop:disable Metrics/LineLength, Style/StringLiterals [The code you want to exclude from the RuboCop check] # rubocop:enable Metrics/LineLength, Style/StringLiterals
You can also disable all checks with:
# rubocop:disable all [Your Ruby code to exclude from all RuboCop checks] # rubocop:enable all
Next I will show you how to configure cops for you app all in one place in a RuboCop configuration file called rubocop.yml.
Configure your .rubocop.yml
Add a file called .rubocop.yml to the root of your project. Then open it to configure which checks you want it to carry out for each file in your project.
The behavior of RuboCop can be controlled via the .rubocop.yml configuration file. It makes it possible to enable/disable certain cops (checks) and to alter their behavior if they accept any parameters.
— Configuration | RuboCop docs
Here is how you might disable all RuboCop checks for specific files or folders.
Exclude: - 'db/**/*' - 'config/**/*' - 'script/**/*'
You can also specify specific checks for certain files using Exclude and Include.
Rails/HasAndBelongsToMany: Exclude: - app/models/problematic.rb Rails/HasAndBelongsToMany: Include: - app/models/*.rb
There are also options for overriding the default values for certain checks. For example, you can override the default method length of 10.
Metrics/MethodLength: Max: 15
Useful RuboCop CLI commands
RuboCop comes with some useful command line options. These include:
rubocop --auto-correct, or using short option rubocop -a Automatically correct some of the easier to amend Ruby style guide recommendations. This is especially useful for the layout/presentation of your code.
rubocop --lintRun only lint checks
rubocop --auto-gen-configGenerate a configuration file acting as a
TODO list. This file is called
A full list of options can be viewed by running
Find out more
Setting up your linter for Ruby code can help you write clear and maintainable applications. Learning how to configure it easily can save you time when using RuboCop checks for certain areas of your application.
To learn more about configuring RuboCop checkout the official RuboCop configuration documentation.