← 返回命令列表

Linux command

rubocop 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Check current directory

rubocop

Check specific files

rubocop [file.rb] [other.rb]

Auto-correct safe issues

rubocop -a

Auto-correct all issues

rubocop -A

Check specific cops

rubocop --only [Layout/LineLength,Style/StringLiterals]

Generate config file

rubocop --init

Generate todo list

rubocop --auto-gen-config

Show offense details

rubocop --extra-details

说明

rubocop is a Ruby static code analyzer and formatter. It enforces style guidelines from the Ruby Style Guide. Cops are individual rules checking specific style violations. Categories include Layout, Style, Lint, Metrics, and Naming. Auto-correct modifies files to fix violations. Safe mode (-a) applies only corrections that don't change behavior. Full mode (-A) includes all corrections. Configuration in .rubocop.yml customizes rules. Cops can be enabled, disabled, or configured with different parameters. The auto-gen-config command creates a todo file ignoring current violations. This enables gradual adoption in existing projects. Integration with editors shows issues in real-time. CI integration enforces style on pull requests.

参数

-a, --auto-correct
Auto-correct safe issues.
-A, --auto-correct-all
Auto-correct all issues.
--only _COPS_
Run specific cops only.
--except _COPS_
Exclude specific cops.
-c _FILE_
Configuration file.
--auto-gen-config
Generate todo config.
--init
Create .rubocop.yml.
-f _FORMAT_
Output format.
--display-cop-names
Show cop names.
--extra-details
Show extra details.
-L, --list-target-files
List files to check.
--parallel
Run in parallel.
--safe-auto-correct
Only safe corrections.

FAQ

What is the rubocop command used for?

rubocop is a Ruby static code analyzer and formatter. It enforces style guidelines from the Ruby Style Guide. Cops are individual rules checking specific style violations. Categories include Layout, Style, Lint, Metrics, and Naming. Auto-correct modifies files to fix violations. Safe mode (-a) applies only corrections that don't change behavior. Full mode (-A) includes all corrections. Configuration in .rubocop.yml customizes rules. Cops can be enabled, disabled, or configured with different parameters. The auto-gen-config command creates a todo file ignoring current violations. This enables gradual adoption in existing projects. Integration with editors shows issues in real-time. CI integration enforces style on pull requests.

How do I run a basic rubocop example?

Run `rubocop` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does -a, --auto-correct do in rubocop?

Auto-correct safe issues.