ruby-refactor
Community Ruby Refactoring Best Practices
Comprehensive refactoring guide for Ruby applications, maintained by the community. Contains 45 rules across 8 categories, prioritized by impact to guide automated refactoring and code generation.
When to Apply
Reference these guidelines when:
- Refactoring Ruby code to reduce complexity and improve design
- Extracting methods, classes, or value objects from large units
- Simplifying complex conditionals and deep nesting
- Reducing coupling between classes and modules
- Adopting idiomatic Ruby patterns and modern Ruby 3.x features
Rule Categories by Priority
| Priority | Category | Impact | Prefix |
|---|---|---|---|
| 1 | Structure & Decomposition | CRITICAL | struct- |
| 2 | Conditional Simplification | CRITICAL | cond- |
| 3 | Coupling & Dependencies | HIGH | couple- |
| 4 | Ruby Idioms | HIGH | idiom- |
| 5 | Data & Value Objects | MEDIUM-HIGH | data- |
| 6 | Design Patterns | MEDIUM | pattern- |
| 7 | Modern Ruby 3.x | MEDIUM | modern- |
| 8 | Naming & Readability | LOW-MEDIUM | name- |
Quick Reference
1. Structure & Decomposition (CRITICAL)
struct-extract-method- Extract Long Methods into Focused Unitsstruct-extract-class- Extract Class for Single Responsibilitystruct-parameter-object- Introduce Parameter Object for Long Signaturesstruct-compose-method- Compose Methods at Single Abstraction Levelstruct-replace-method-with-object- Replace Complex Method with Method Objectstruct-single-responsibility- One Reason to Change per Classstruct-flatten-deep-nesting- Flatten Deep Nesting with Early Extraction
2. Conditional Simplification (CRITICAL)
cond-guard-clauses- Replace Nested Conditionals with Guard Clausescond-decompose-conditional- Extract Complex Booleans into Named Predicatescond-replace-with-polymorphism- Replace case/when with Polymorphismcond-null-object- Replace nil Checks with Null Objectcond-pattern-matching- Use Pattern Matching for Structural Conditionscond-consolidate-duplicates- Consolidate Duplicate Conditional Fragments
3. Coupling & Dependencies (HIGH)
couple-law-of-demeter- Enforce Law of Demeter with Delegationcouple-feature-envy- Move Method to Resolve Feature Envycouple-dependency-injection- Inject Dependencies via Constructor Defaultscouple-composition-over-inheritance- Replace Mixin with Composed Objectcouple-tell-dont-ask- Tell Objects What to Do, Don't Query Their Statecouple-avoid-class-methods-domain- Avoid Class Methods in Domain Logic
4. Ruby Idioms (HIGH)
idiom-prefer-enumerable- Use map/select/reject Over each with Accumulatoridiom-keyword-arguments- Use Keyword Arguments for Clarityidiom-duck-typing- Use respond_to? Over is_a? for Type Checkingidiom-predicate-methods- Name Boolean Methods with ? Suffixidiom-respond-to-missing- Always Pair method_missing with respond_to_missing?idiom-block-yield- Use yield Over block.call for Simple Blocksidiom-implicit-return- Omit Explicit return for Last Expression
5. Data & Value Objects (MEDIUM-HIGH)
data-value-object- Replace Primitive Obsession with Value Objectsdata-define-immutable- Use Data.define for Immutable Value Objectsdata-encapsulate-collection- Encapsulate Collections Behind Domain Methodsdata-replace-data-clump- Replace Data Clumps with Grouped Objectsdata-separate-query-command- Separate Query Methods from Command Methods
6. Design Patterns (MEDIUM)
pattern-strategy- Extract Algorithm Variations into Strategy Objectspattern-factory- Use Factory Method to Abstract Object Creationpattern-template-method- Define Algorithm Skeleton with Template Methodpattern-decorator- Wrap Objects with Decorator for Added Behaviorpattern-null-object-protocol- Implement Null Object with Full Protocol
7. Modern Ruby 3.x (MEDIUM)
modern-pattern-matching- Use case/in for Structural Pattern Matchingmodern-deconstruct-keys- Implement deconstruct_keys for Custom Pattern Matchingmodern-endless-methods- Use Endless Method Definition for Simple Methodsmodern-hash-pattern-guard- Use Pattern Matching with Guard Clausesmodern-rightward-assignment- Use Rightward Assignment for Pipeline Expressions
8. Naming & Readability (LOW-MEDIUM)
name-intention-revealing- Use Intention-Revealing Namesname-consistent-vocabulary- Use One Word per Concept Across Codebasename-avoid-abbreviations- Spell Out Names Except Universal Abbreviationsname-rename-to-remove-comments- Rename to Eliminate Need for Comments
How to Use
Read individual reference files for detailed explanations and code examples:
- Section definitions - Category structure and impact levels
- Rule template - Template for adding new rules
Reference Files
| File | Description |
|---|---|
| references/_sections.md | Category definitions and ordering |
| assets/templates/_template.md | Template for new rules |
| metadata.json | Version and reference information |
More from pproenca/dot-skills
zod
Zod schema validation best practices for type safety, parsing, and error handling. This skill should be used when defining z.object schemas, using z.string validations, safeParse, or z.infer. This skill does NOT cover React Hook Form integration patterns (use react-hook-form skill) or OpenAPI client generation (use orval skill).
2.0Kclean-architecture
Clean Architecture principles and best practices from Robert C. Martin's book. This skill should be used when designing software systems, reviewing code structure, or refactoring applications to achieve better separation of concerns. Triggers on tasks involving layers, boundaries, dependency direction, entities, use cases, or system architecture.
1.4Kemilkowal-animations
Emil Kowalski's animation best practices for web interfaces. Use when writing, reviewing, or implementing animations in React, CSS, or Framer Motion. Triggers on tasks involving transitions, easing, gestures, toasts, drawers, or motion.
917vitest
Vitest testing framework patterns for test setup, async testing, mocking with vi.*, snapshots, and test performance (formerly test-vitest). This skill should be used when writing or debugging Vitest tests. This skill does NOT cover TDD methodology (use test-tdd skill), API mocking with MSW (use test-msw skill), or Jest-specific APIs.
906typescript
This skill should be used when the user asks to "optimize TypeScript performance", "speed up tsc compilation", "configure tsconfig.json", "fix type errors", "improve async patterns", or encounters TS errors (TS2322, TS2339, "is not assignable to"). Also triggers on .ts, .tsx, .d.ts file work involving type definitions, module organization, or memory management. Does NOT cover TypeScript basics, framework-specific patterns, or testing.
820nuqs
nuqs (type-safe URL query state) best practices for Next.js applications. This skill should be used when writing, reviewing, or refactoring code that uses nuqs for URL state management. Triggers on tasks involving useQueryState, useQueryStates, search params, URL state, query parameters, nuqs parsers, or Next.js routing with state.
734