Rubocop

From el-feo/ai-context · View interactive version

Rubocop is an AI agent skill from the GitHub repository el-feo/ai-context. Ruby static code analyzer and formatter for enforcing style guidelines, detecting bugs, and improving code quality. Supports Rails, RSpec, and Performance extensions with safe autocorrection capabilities.

How to use this skill

Skill file

RuboCop Code Analysis & Formatting

RuboCop is Ruby's premier static code analyzer (linter) and formatter, based on the community-driven Ruby Style Guide. This skill enables comprehensive code analysis, automatic formatting, and enforcement of coding standards across Ruby projects including Rails applications.

When to Use This Skill

Claude automatically uses this skill when users:

Core Capabilities

Code Analysis & Linting

Automatic Code Correction

Configuration & Customization

Extension Integration

Quick Analysis Workflow

Run Basic Analysis

# Analyze current directory
rubocop

# Analyze specific files/directories
rubocop app spec lib/important_file.rb

# Show only correctable offenses
rubocop --display-only-correctable

Apply Corrections

# Safe autocorrection only (recommended)
rubocop -a

# All corrections including unsafe
rubocop -A

# Layout/formatting corrections only
rubocop -x

# Autocorrect specific cops
rubocop -a --only Style/StringLiterals,Layout/TrailingWhitespace

Targeted Analysis

# Run only specific cops
rubocop --only Style/StringLiterals,Naming/MethodName

# Run all cops except specified
rubocop --except Metrics/MethodLength

# Run only lint cops
rubocop --lint

# Check only Rails-specific issues
rubocop --only Rails

Configuration Essentials

Basic .rubocop.yml Setup

# Enable extensions
plugins:
  - rubocop-rails
  - rubocop-rspec
  - rubocop-performance

AllCops:
  TargetRubyVersion: 3.2
  NewCops: enable  # Auto-enable new cops
  Exclude:
    - 'db/schema.rb'
    - 'vendor/**/*'
    - 'node_modules/**/*'

# Adjust specific cops
Style/StringLiterals:
  EnforcedStyle: double_quotes

Metrics/MethodLength:
  Max: 15
  Exclude:
    - 'spec/**/*'

Common Configuration Patterns

Inherit from shared config:

inherit_from:
  - .rubocop_todo.yml
  - config/rubocop_defaults.yml

Rails-specific settings:

Rails:
  Enabled: true

Rails/ApplicationRecord:
  Enabled: true
  Exclude:
    - 'db/migrate/**'

RSpec configuration:

RSpec/ExampleLength:
  Max: 10

RSpec/MultipleExpectations:
  Max: 3

Analysis Interpretation

Understanding Offense Output

app/models/user.rb:15:3: C: Style/StringLiterals: Prefer single-quoted strings...
  name = "John Doe"
         ^^^^^^^^^^

Format breakdown:

Severity Levels

Common Workflows

Initial Project Setup

# Generate initial configuration
rubocop --init

# Generate .rubocop_todo.yml for existing violations
rubocop --auto-gen-config

# Use todo file to gradually fix issues
# .rubocop.yml:
inherit_from: .rubocop_todo.yml

Pre-commit Integration

# Check staged files only
git diff --name-only --cached | grep '\.rb$' | xargs rubocop

# With autocorrection
git diff --name-only --cached | grep '\.rb$' | xargs rubocop -a

CI/CD Integration

# Exit with error code if offenses found
rubocop --format progress --fail-level warning

# Generate formatted reports
rubocop --format json --out rubocop-report.json
rubocop --format html --out rubocop-report.html

Parallel Execution

# Use all available CPUs (enabled by default)
rubocop --parallel

# Limit CPU usage
PARALLEL_PROCESSOR_COUNT=2 rubocop

Extension-Specific Features

Rails Analysis

Detects Rails-specific issues:

RSpec Analysis

Enforces RSpec best practices:

Performance Analysis

Identifies performance optimizations:

Troubleshooting

"Unknown cop" errors:

Autocorrection not working:

Performance issues:

Additional Resources

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.