ruby-patterns

Provide Ruby idioms, design patterns, and metaprogramming guidance for code quality.

8|2|Updated Oct 17, 2025
One-click install
npx skills add https://github.com/geoffjay/claude-plugins --skill ruby-patterns
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: ruby-patterns
Source: https://github.com/geoffjay/claude-plugins/tree/main/plugins/ruby-sinatra-advanced/skills/ruby-patterns
Command: npx skills add https://github.com/geoffjay/claude-plugins --skill ruby-patterns

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes assets (resource) and references (resource) components.

What problem does it solve?

Writing clean, efficient, and maintainable Ruby code requires familiarity with common idioms, established design patterns, and powerful metaprogramming techniques. Without this knowledge, code can become verbose, inefficient, or difficult to extend.

Core Features & Use Cases

  • Common Idioms: Quick reference for conditional assignment, array/hash shortcuts, enumerable methods, and string operations for concise and expressive code.
  • Design Patterns: Detailed instructions for Creational (Factory, Builder, Singleton), Structural (Decorator, Adapter, Composite), and Behavioral (Strategy, Observer, Command) patterns.
  • Metaprogramming Techniques: Guidance on dynamic method definition, method_missing, class macros (DSLs), and module inclusion hooks for flexible and powerful code.
  • Use Case: A Ruby developer is refactoring a legacy application and wants to improve its structure, readability, and performance. This Skill provides a catalog of patterns and techniques, from simple idioms to advanced metaprogramming, to elevate the codebase to modern Ruby standards.

Quick Start

Conditional Assignment (set if nil)

value ||= default_value

Array creation shortcut

%w[apple banana orange] # => ["apple", "banana", "orange"]

Enumerable transformation

array.map(&:upcase) # => ["APPLE", "BANANA"]

Guard Clause

def process(user) return unless user&.active?

Main logic

end

Frequently Asked Questions about ruby-patterns

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I refactor Ruby code to use modern idioms and patterns?

Modern Ruby idioms improve code readability through safe navigation operators, guard clauses, and symbol-to-proc conversions. Design patterns like Factory, Builder, and Decorator provide proven structures for solving recurring problems, while metaprogramming techniques enable DSLs and flexible abstractions to reduce verbosity and enhance maintainability across services and libraries.

What are the most common Ruby design patterns and when should I use them?

Ruby supports creational patterns (Factory, Builder, Singleton) for object instantiation, structural patterns (Decorator, Adapter, Composite) for composing objects, and behavioral patterns (Strategy, Observer, Command) for managing responsibilities. Each pattern solves specific structural or behavioral problems; select based on whether you're creating objects, organizing relationships, or managing state and behavior.

How does metaprogramming in Ruby improve code quality?

Ruby metaprogramming—dynamic method definition, method_missing, class macros, and module hooks—eliminates boilerplate, creates domain-specific languages (DSLs), and enables flexible abstractions. These techniques make code more expressive and maintainable when used deliberately, though they require careful documentation and testing to remain understandable.

What's the best way to write concise, idiomatic Ruby?

Idiomatic Ruby leverages built-in shortcuts: conditional assignment (||=), array/hash literals (%w for word arrays), and Enumerable methods with symbol-to-proc (&:method_name). These patterns make code more expressive and familiar to Ruby developers, reducing cognitive load and improving readability without sacrificing performance.

Can I apply design patterns to legacy Ruby code without rewriting everything?

Yes. Refactor legacy code incrementally by identifying repetitive structures or violations of single responsibility, then apply targeted patterns. Decorator and Adapter patterns work well for wrapping existing behavior; Strategy extracts conditional logic; module inclusion introduces shared behavior. Test thoroughly after each change to ensure stability.

When should I avoid metaprogramming in Ruby?

Avoid metaprogramming when a straightforward solution exists; it obscures code intent and complicates debugging. Limit method_missing and dynamic method definition to specific, well-documented use cases. Prioritize clarity and testability; use metaprogramming only when its benefits in reducing duplication or enabling flexible abstractions outweigh the added complexity.