skillfed

ruby

Master Ruby language conventions and modern features (3.x+) for writing clean, performant code. This skill covers error handling strategies, pattern matching, and Ruby idioms like guard clauses and safe navigation. Use it when writing pure Ruby, optimizing performance, or establishing team conventions—separate skills handle Rails, design patterns, testing, and style.

Ruby helps you write idiomatic code using modern 3.x+ conventions, error handling patterns, and performance techniques.

AI-generated summary based on this skill's SKILL.md

12 2 MIT updated by el-feo

Install

el-feo/ai-context/ruby · repository language: Ruby

git clone https://github.com/el-feo/ai-context
cp -r ai-context/plugins/ruby-rails/skills/ruby ~/.claude/skills/ruby
npx skillfed install el-feo/ai-context/ruby

Frequently asked questions

AI-generated answers based on this skill's SKILL.md and metadata

How do I write idiomatic Ruby code following modern conventions?

Ruby emphasizes readability and expressiveness. Write idiomatic Ruby by using guard clauses to exit early, leveraging the safe navigation operator (&.) to avoid nil checks, and naming predicate methods with a trailing question mark. Prefer `hash.fetch(:key)` over bracket access for safer defaults, use frozen string literals to reduce memory overhead, and embrace blocks and iterators rather than imperative loops. Ruby 3.x+ idioms also include pattern matching for elegant case statements and lazy enumerables for memory-efficient transformations on large datasets.

What are ruby error handling patterns and best practices?

Ruby error handling patterns emphasize custom exception hierarchies and result objects. Define domain-specific exceptions inheriting from StandardError to signal specific failure modes. Use `raise` (not `fail`) to throw exceptions, and rescue specific exception types rather than catching all errors. For operations that may fail without exceptional circumstances, return result objects—lightweight wrappers holding success/failure state and values. This separates error signaling (exceptions for truly exceptional cases) from expected failure handling (result objects for business logic failures), improving code clarity and testability.

What ruby 3.x idioms and conventions should I follow?

Ruby 3.x introduces pattern matching with `case/in` syntax for destructuring complex data structures elegantly. Use data classes (via Struct or Data) to define immutable value objects with minimal boilerplate. Adopt frozen string literals by default to reduce garbage collection pressure. Leverage the safe navigation operator (&.) and the endless method syntax (`def name = value`) for concise definitions. Prefer keyword arguments over positional ones for clarity, and use numbered block parameters (`_1`, `_2`) in single-line blocks. These conventions make Ruby 3.x code more expressive and performant.

How can I optimize Ruby performance with frozen strings and lazy enums?

Ruby performance optimization starts with frozen string literals—add `# frozen_string_literal: true` at file tops to prevent string duplication and reduce memory. Use lazy enumerables (`Enumerator::Lazy`) when chaining transformations on large collections; they defer computation until terminal operations, avoiding intermediate arrays. Memoize expensive calculations using instance variables or the `||=` pattern. Avoid string concatenation in loops; use `String#<<` or `Array#join` instead. Profile with `ruby-prof` to identify bottlenecks, and consider using `Fiber` or `Ractor` for concurrent workloads in Ruby 3.x+.

What are ruby data class immutable objects and pattern matching examples?

Ruby data classes create immutable value objects with minimal syntax. Use `Data.define(:field1, :field2)` (Ruby 3.2+) or `Struct.new` to define frozen records. Pattern matching with `case/in` destructures these objects elegantly: `case user; in Data(name:, age:) if age >= 18; ...; end`. This replaces verbose conditional logic with readable guards. Data classes pair naturally with pattern matching to handle complex domain models—each case branch extracts only needed fields, reducing intermediate variables and improving maintainability.

What's the difference between fail and raise in Ruby?

Ruby's `raise` and `fail` are aliases—both throw exceptions identically. Modern Ruby style prefers `raise` because it's more explicit about intent: you're raising an error condition. `fail` is a legacy convention from older Ruby versions and is rarely used in contemporary code. Always use `raise` with a specific exception class and message: `raise ArgumentError, 'invalid input'`. This clarity helps maintainers understand that an exceptional condition occurred, not a normal control flow decision.

SKILL.md

rendered from the published skill — quoted content, verbatim

Ruby Language Skill

Error Handling Conventions

Weirich raise/fail Convention

Use fail for first-time exceptions, raise only for re-raising:

def process(order)
  fail ArgumentError, "Order cannot be nil" if order.nil?

  begin
    gateway.charge(order)
  rescue PaymentError =&gt; e
    logger.error("Payment failed: #{e.message}")
    raise  # re-raise with raise
  end
end
Custom Exception Hierarchies

Group domain exceptions under a base error:

```ruby module MyApp class Error < StandardError; end class PaymentError < Error; end class InsufficientFundsError < PaymentError; end end

Rescue at any

(truncated - see the full file via the links below)

Read as markdown · JSON record · Browse the source repository

File tree — 4 files
plugins/ruby-rails/skills/ruby/SKILL.md
plugins/ruby-rails/skills/ruby/references/error_handling.md
plugins/ruby-rails/skills/ruby/references/modern_ruby.md
plugins/ruby-rails/skills/ruby/references/performance.md

Related skills

Tags

language-fundamentals code-style-guide exception-design performance-tuning syntax-modernization functional-patterns memory-efficiency type-safety