# Ruby

> Ruby best practices for clean code and Rails development. Trigger: When writing Ruby code or Rails applications.

- Skill: `odjaramillo/ruby` (Agent Skill, multi-file: 2 files)
- Install (CLI): `npx skillmds@latest add odjaramillo/ruby`
- Raw SKILL.md: https://api.skillmd.com/api/skills/odjaramillo/ruby/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- License: Apache-2.0
- Author: odjaramillo (https://skillmd.com/u/odjaramillo)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/odjaramillo/ruby

---


## Critical Patterns

### Ruby Idioms (REQUIRED)

```ruby
# ✅ ALWAYS: Use Ruby idioms
users.map(&:name)                    # Symbol to proc
user&.address&.city                  # Safe navigation
name ||= "Default"                   # Memoization
```

### Class Structure (REQUIRED)

```ruby
# ✅ ALWAYS: Clear class structure
class User
  attr_reader :name, :email

  def initialize(name:, email:)
    @name = name
    @email = email
  end

  def admin?
    role == 'admin'
  end
end
```

### Error Handling (REQUIRED)

```ruby
# ✅ Use custom exceptions
class UserNotFoundError < StandardError; end

def find_user!(id)
  user = User.find_by(id: id)
  raise UserNotFoundError, "User #{id} not found" unless user
  user
end
```

---

## Decision Tree

```
Need attribute access?     → Use attr_reader/writer
Need boolean check?        → Use predicate method (?)
Need dangerous operation?  → Use bang method (!)
Need configuration?        → Use block with yield
```

---

## Commands

```bash
ruby script.rb             # Run script
irb                        # Interactive Ruby
bundle install             # Install dependencies
rspec                      # Run tests
rubocop                    # Lint code
```

---

## Resources

- **Clean Code**: [clean-code.md](clean-code.md)
- **TDD with RSpec**: [tdd-rspec.md](tdd-rspec.md)

