Ruby

From G1Joshi/Agent-Skills · View interactive version

Ruby is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert Ruby programming assistance covering blocks, metaprogramming, Bundler, gems, and modern Ruby 3.x features. Use when developing Rails web apps, writing CLI utilities, or managing Ruby gems.

How to use this skill

Skill file

Ruby

A dynamic, interpreted language known for its elegant syntax.

When to Use

Quick Start

puts "Hello, World!"

class Greeter
  def initialize(name)
    @name = name
  end

  def say_hi
    puts "Hi #{@name}!"
  end
end

g = Greeter.new("Alice")
g.say_hi

Core Concepts

Object-Oriented Everything & Dynamic Metaprogramming

Every value in Ruby is a full-fledged object; classes can be modified dynamically at runtime:

class Integer
  def to_usd
    "$#{format('%.2f', self)}"
  end
end

puts 100.to_usd # "$100.00"

Blocks, Procs & Enumerable Power

Passes executable code blocks to methods for clean data manipulation:

orders = [
  { id: 1, total: 45.0, status: :completed },
  { id: 2, total: 120.0, status: :pending },
  { id: 3, total: 85.0, status: :completed }
]

completed_revenue = orders
  .select { |o| o[:status] == :completed }
  .sum { |o| o[:total] }

puts "Completed Revenue: $#{completed_revenue}" # 130.0

Modern Concurrency (Fibers & Ractor)

Runs true parallel execution without Global VM Lock (GVL) contention using Ractors (Ruby 3+):

# Parallel computation across multiple CPU cores
r = Ractor.new do
  # Completely isolated memory space
  1_000_000.times.reduce(:+)
end

puts "Ractor Result: #{r.take}"

Common Patterns

Pattern Matching with Value Extraction (Ruby 3+)

Problem: Verbose nested if/elsif conditionals when handling structured JSON API responses.

Solution:

Use Ruby 3 native pattern matching:

def handle_response(response)
  case response
  in { status: 200, data: { user: { name:, email: } } }
    puts "User found: #{name} <#{email}>"
  in { status: 404, error: msg }
    warn "Not found: #{msg}"
  in { status: (500..) }
    raise "Server error occurred"
  else
    puts "Unknown response format"
  end
end

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :------------------------------------------------------- | :-------------------------------------------------- | :---------------------------------------------------------------------------- |

| NoMethodError: undefined method '...' for nil:NilClass | Calling method on a nil object. | Use safe navigation operator object&.method_name. |

| LoadError: cannot load such file -- ... | Gem not installed or missing in Gemfile. | Run bundle install and execute script via bundle exec ruby script.rb. |

| NameError: uninitialized constant | Constant/Class not required or module nesting typo. | Check file require paths and ensure file naming matches Zeitwerk conventions. |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.