AgentSkillsCN

linting-ruby

了解 Rubocop 的代码风格规范,以及何时应禁用特定规则。在处理代码风格违规时,此技能能助你事半功倍。

SKILL.md
--- frontmatter
name: linting-ruby
description: Rubocop linting conventions and when to disable rules. Use when addressing lint violations.

Rubocop Linting

Run linting before committing:

bash
shadowenv exec -- dev style           # Check style
shadowenv exec -- dev style --fix     # Auto-fix issues

Disabling Rules

Only disable rules per-line with a comment explaining why:

ruby
# rubocop:disable Rails/SkipsModelValidations -- counter update doesn't need validations
record.update_columns(counter: record.counter + 1)
# rubocop:enable Rails/SkipsModelValidations

Acceptable Exceptions

These cops can be disabled when justified:

CopWhen to Disable
Rails/SkipsModelValidationsCounter updates, bulk operations where validations aren't needed
Metrics/AbcSizeComplex but readable methods that can't be simplified
Metrics/MethodLengthMethods with many simple steps (e.g., building a hash)
Style/GuardClauseWhen early return hurts readability

Red Flags

If you're disabling the same cop repeatedly, the code pattern needs rethinking:

  • Multiple Metrics/ disables = method needs refactoring
  • Multiple Rails/ disables = consider a different approach
  • Disabling in a test file = tests may be too complex

Project-Specific Rules

Check .rubocop.yml for project overrides. Some teams have different conventions.