In a .rubocop.yml file, how do you disable a cop, change one of its options and exclude files, and what is a department?
answer
- Department/CopName as the YAML key
- Enabled: false under the cop
- options such as Max or EnforcedStyle
- AllCops Exclude vs a cop's own Exclude
- --show-cops prints the effective settings
basics
~20 sA .rubocop.yml keys each cop by qualified name, such as Layout/LineLength: Enabled: false switches it off, keys like Max tune it, and Exclude skips paths. A department (Lint, Style, Layout, Metrics...) groups cops and can be configured as one.
solid answer
~40 sRuboCop starts from the defaults in its own `config/default.yml`; `.rubocop.yml` only lists the differences. Each cop is keyed by its qualified name, `Department/CopName`. Under it, `Enabled: false` turns the cop off, cop-specific options tune it (`Layout/LineLength: Max: 100`, `Style/StringLiterals: EnforcedStyle: double_quotes`), and `Exclude` lists paths that one cop skips. `AllCops: Exclude` skips paths for every cop. A **department** is the prefix: `Lint`, `Style`, `Layout`, `Metrics`, `Naming`, `Security`, `Bundler`, `Gemspec`, `Migration`. A department key such as `Metrics: Enabled: false` switches the whole group off, and a single cop inside it can still be turned back on with its own `Enabled: true`. A misspelled cop name stops the run with an `unrecognized cop or department` error, and `rubocop --show-cops Layout/LineLength` prints what actually applies.
code
yaml · 20 lines# .rubocop.yml
AllCops:
Exclude:
- "db/schema.rb"
- "bin/**/*"
- "vendor/**/*" # repeated: this list replaces the default one
Style/StringLiterals:
EnforcedStyle: double_quotes
Layout/LineLength:
Max: 100
Exclude:
- "config/routes.rb"
Metrics:
Enabled: false
Metrics/CyclomaticComplexity:
Enabled: truego deeper
Recall the Department/CopName key, Enabled: false, one option such as Max or EnforcedStyle, and the difference between AllCops Exclude and a cop's own Exclude.
Explain that the file only overrides config/default.yml, that the nearest .rubocop.yml wins for a file, and that a list you write replaces the inherited list.
Show how you verify a config instead of guessing: --show-cops for effective settings, -L for the file set, and --force-exclusion for tools that pass files by name.
Treat .rubocop.yml as a reviewed policy file: every disabled cop or raised Max is a team decision that should carry a comment saying why.
## What the file is **RuboCop** is a static code analyser and formatter for Ruby. Each individual check is a **cop**, and every cop ships with default settings in the gem's own `config/default.yml`. A project's **`.rubocop.yml`** is a YAML file that lists only the settings that differ from those defaults; everything you do not mention keeps its default value. RuboCop picks the configuration for a file by looking for the **nearest** `.rubocop.yml`, starting in the inspected file's directory and walking upwards. If none is found in the project it falls back to `.config/.rubocop.yml` at the project root, then `~/.rubocop.yml`, then `~/.config/rubocop/config.yml`, and finally the built-in defaults. Passing `--config FILE` on the command line replaces that search with one explicit file. ## Departments and cop names Every cop has a **qualified name** of the form `Department/CopName`, and that name is the YAML key you configure it under. The **department** is the group the cop belongs to. RuboCop 1.91 itself ships nine: | Department | What its cops look at | Default severity | |---|---|---| | `Lint` | likely bugs and ambiguous code | `warning` | | `Security` | dangerous calls such as `eval` with dynamic input | `warning` | | `Metrics` | size and complexity (method length, ABC size) | `refactor` | | `Layout` | whitespace, indentation, alignment, line length | `convention` | | `Style` | idiom and consistency choices | `convention` | | `Naming` | names of methods, variables, constants, files | `convention` | | `Bundler` | the `Gemfile` | `convention` | | `Gemspec` | `*.gemspec` files | `convention` | | `Migration` | department-qualified cop names in `rubocop:` directive comments | `convention` | A few cops override their department's severity; `Bundler/DuplicatedGem`, for example, reports at `warning`. Extension gems add their own departments (for example `Performance` from rubocop-performance). Writing the bare cop name without its department still works when the name is unique, but the qualified form is the recommended one. ## The keys you set under a cop - **`Enabled`** - `true` or `false`. A few cops ship disabled; newly added cops ship with the special value `pending` until you decide. - **Cop-specific options** - each cop documents its own: `Max` for `Layout/LineLength` and the `Metrics` cops, `EnforcedStyle` for style choices such as `Style/StringLiterals` (default `single_quotes`), `AllowedMethods` and `AllowedPatterns` for exemptions. - **`Include` / `Exclude`** - path globs this one cop inspects or skips. - **`Severity`** - the level the cop's offenses are reported at (`info`, `refactor`, `convention`, `warning`, `error`, `fatal`). - **`AutoCorrect`** - `always`, `contextual` or `disabled`, which limits whether the cop may rewrite code. A **department key** accepts the same common settings for all its cops at once. Precedence works in the cop's favour: 1. `Style: Enabled: false` turns every `Style` cop off. 2. `Style/Alias: Enabled: true` in the same file (or a file inheriting from it) turns that one cop back on. 3. Every other `Style` cop stays off. ## Excluding files - **`AllCops: Exclude`** removes paths from inspection for every cop. The default list is `node_modules/**/*`, `tmp/**/*`, `vendor/**/*` and `.git/**/*`. - **A cop's own `Exclude`** removes paths for that cop only; other cops still inspect the file. - A list you write **replaces** the list it inherits, so an `AllCops: Exclude` that names only `db/schema.rb` drops the `vendor/**/*` default unless you repeat it. - Paths in `.rubocop.yml` are relative to the directory the file sits in. - Files passed **explicitly** on the command line are inspected even when excluded; `--force-exclusion` makes RuboCop honour `Exclude` for them too, which matters for editor and pre-commit integrations that pass single files. ## Checking what you configured - `rubocop --show-cops Layout/LineLength` prints the cop's effective settings for the current directory, after all inheritance. - `rubocop -L` (`--list-target-files`) lists the files a run will inspect, which is the quickest way to test an `Exclude` glob. - `rubocop --list-enabled-cops-for app/models` lists the cops that will run on a path. - A key that is not a known cop or department makes the run fail with `unrecognized cop or department ... found in .rubocop.yml`; `--ignore-unrecognized-cops` turns that into a warning. - `Lint/Syntax` is the one cop you cannot configure: any setting for it is rejected, because it reports files that do not parse.
- What happens when a .rubocop.yml misspells a cop name, for example Layout/LineLenght?RuboCop validates the configuration before inspecting anything and stops with `unrecognized cop or department Layout/LineLenght found in .rubocop.yml`, so the typo fails the run instead of being silently ignored. `--ignore-unrecognized-cops` downgrades the error to a warning, which helps when one shared file is read by several RuboCop versions and a newer cop does not exist in the older one yet.
- Why does a pre-commit hook report offenses in a file that AllCops Exclude lists?The hook passes changed files to `rubocop` by name, and RuboCop inspects files given explicitly on the command line even when they match `AllCops: Exclude`. Adding `--force-exclusion` to the hook's command makes RuboCop apply the `Exclude` patterns to explicit arguments as well, so generated or vendored files stay skipped.
saying these in an interview costs you the question
- A misspelled cop name in .rubocop.yml is silently ignored.
- Disabling a department means no cop in it can be switched back on.
- A cop's own Exclude list also stops every other cop from inspecting that file.
- RuboCop only reads the .rubocop.yml at the project root, never one in a subdirectory.
- Lint/Syntax can be disabled in .rubocop.yml like any other cop.