Back to Skills

ruby-patterns

verified

Modern Ruby idioms, design patterns, metaprogramming techniques, and best practices. Use when writing Ruby code or refactoring for clarity.

View on GitHub

Marketplace

geoffjay-claude-plugins

geoffjay/claude-plugins

Plugin

ruby-sinatra-advanced

languages

Repository

geoffjay/claude-plugins
7stars

plugins/ruby-sinatra-advanced/skills/ruby-patterns/SKILL.md

Last Verified

January 20, 2026

Install Skill

Select agents to install to:

Scope:
npx add-skill https://github.com/geoffjay/claude-plugins/blob/main/plugins/ruby-sinatra-advanced/skills/ruby-patterns/SKILL.md -a claude-code --skill ruby-patterns

Installation paths:

Claude
.claude/skills/ruby-patterns/
Powered by add-skill CLI

Instructions

# Ruby Patterns Skill

## Tier 1: Quick Reference - Common Idioms

### Conditional Assignment

```ruby
# Set if nil
value ||= default_value

# Set if falsy (nil or false)
value = value || default_value

# Safe navigation
user&.profile&.avatar&.url
```

### Array and Hash Shortcuts

```ruby
# Array creation
%w[apple banana orange]  # ["apple", "banana", "orange"]
%i[name email age]        # [:name, :email, :age]

# Hash creation
{ name: 'John', age: 30 }  # Symbol keys
{ 'name' => 'John' }       # String keys

# Hash access with default
hash.fetch(:key, default)
hash[:key] || default
```

### Enumerable Shortcuts

```ruby
# Transformation
array.map(&:upcase)
array.select(&:active?)
array.reject(&:empty?)

# Aggregation
array.sum
array.max
array.min
numbers.reduce(:+)

# Finding
array.find(&:valid?)
array.any?(&:present?)
array.all?(&:valid?)
```

### String Operations

```ruby
# Interpolation
"Hello #{name}!"

# Safe interpolation
"Result: %{value}" % { value: result }

# Multiline
<<~TEXT
  Heredoc with indentation
  removed automatically
TEXT
```

### Block Syntax

```ruby
# Single line - use braces
array.map { |x| x * 2 }

# Multi-line - use do/end
array.each do |item|
  process(item)
  log(item)
end

# Symbol to_proc
array.map(&:to_s)
array.select(&:even?)
```

### Guard Clauses

```ruby
def process(user)
  return unless user
  return unless user.active?

  # Main logic here
end
```

### Case Statements

```ruby
# Traditional
case status
when 'active'
  activate
when 'inactive'
  deactivate
end

# With ranges
case age
when 0..17
  'minor'
when 18..64
  'adult'
else
  'senior'
end
```

---

## Tier 2: Detailed Instructions - Design Patterns

### Creational Patterns

**Factory Pattern:**
```ruby
class UserFactory
  def self.create(type, attributes)
    case type
    when :admin
      AdminUser.new(attributes)
    when :member
      MemberUser.new(attributes)
    when :guest
      GuestUser.new(attributes)
    else
      raise ArgumentError, "Unknown user type: #{ty

Validation Details

Front Matter
Required Fields
Valid Name Format
Valid Description
Has Sections
Allowed Tools
Instruction Length:
13020 chars