LinuxCommandLibrary

rubocop

Analyze and enforce Ruby code style

TLDR

Check all files in the current directory (including subdirectories)

$ rubocop
copy

Check one or more specific files or directories
$ rubocop [path/to/file_or_directory1 path/to/file_or_directory2 ...]
copy

Write output to file
$ rubocop --out [path/to/file]
copy

View list of cops (linter rules)
$ rubocop --show-cops
copy

Exclude a cop
$ rubocop --except [cop1 cop2 ...]
copy

Run only specified cops
$ rubocop --only [cop1 cop2 ...]
copy

Auto-correct files (experimental)
$ rubocop --auto-correct
copy

SYNOPSIS

rubocop [options] [files or directories...]

PARAMETERS

-a, --auto-correct
    Automatically correct offenses whenever possible. This is a powerful feature for quick fixes.

--auto-gen-config
    Generate a configuration file (`.rubocop_todo.yml`) based on current offenses, allowing you to gradually fix issues.

--config
    Specify a custom configuration file to use instead of the default `.rubocop.yml`.

--display-cop-names
    Display the name of the cop that triggered each offense in the output.

--format
    Choose an output formatter (e.g., `simple`, `progress`, `json`, `html`). Defaults to `progress`.

-o, --out
    Write the output of the analysis to the specified file instead of standard output.

--only
    Run only the specified cops. Useful for focusing on particular rule sets.

--except
    Run all cops except the specified ones.

--parallel
    Run cops in parallel, which can significantly speed up analysis on large codebases.

--show-cops [cop1,cop2,...]
    Display details about all available cops or specific cops, including their description and configuration options.

--init
    Generate a basic `.rubocop.yml` file in the current directory, helping to quickly set up RuboCop.

-v, --version
    Display the version of RuboCop currently installed.

-h, --help
    Display help information for the `rubocop` command.

DESCRIPTION

RuboCop is a powerful Ruby static code analyzer and code formatter that enforces the community-driven Ruby Style Guide. It helps maintain consistency, improve code quality, and reduce common errors in Ruby projects.

It operates by running a series of individual checks, known as 'cops,' which cover various aspects of code, including style, linting, metrics, and security. Users can enable, disable, or configure these cops through a `.rubocop.yml` configuration file, allowing for highly customized enforcement rules tailored to specific project needs or team preferences.

Beyond simply reporting offenses, RuboCop also offers auto-correction capabilities for many issues, significantly streamlining the code review and refactoring process. It's widely adopted in the Ruby ecosystem and can be easily integrated into development workflows, CI/CD pipelines, and pre-commit hooks.

CAVEATS

While highly configurable, RuboCop can sometimes be opinionated, requiring significant adjustments to its configuration file to align with specific project styles not covered by the default style guide. Auto-correction is powerful but not always perfect; it's essential to review changes. Performance can be a concern on very large codebases without parallelization or careful exclusion of files.

CONFIGURATION WITH .RUBOCOP.YML

The behavior of RuboCop is primarily controlled by a `.rubocop.yml` file located in the project root or parent directories. This YAML file allows users to enable/disable specific cops, customize their parameters, exclude files or directories from analysis, and inherit configurations from other `.rubocop.yml` files (e.g., from shared style guides). This flexibility is key to adapting RuboCop to diverse project requirements.

UNDERSTANDING 'COPS'

In RuboCop, a 'cop' is an individual rule or check. Cops are organized into departments, such as Style (e.g., `Style/HashSyntax`), Lint (for potential errors, e.g., `Lint/UselessAssignment`), Metrics (for code complexity, e.g., `Metrics/MethodLength`), and Security (for potential vulnerabilities, e.g., `Security/Eval`). Each cop checks for a specific aspect of code quality or style, and many offer auto-correction capabilities.

HISTORY

RuboCop was created by Bozhidar Batsov and first released in 2012. It quickly gained traction within the Ruby community due to its adherence to the widely accepted Ruby Style Guide and its comprehensive set of checks. It evolved to become the de facto standard for Ruby code style enforcement and static analysis, continually maintained and improved by a vibrant open-source community.

SEE ALSO

git(1) (for pre-commit hooks), rake(1) (often used to integrate RuboCop tasks), eslint(1) (JavaScript linter), flake8(1) (Python linter)

Copied to clipboard