ruby – Coding skill for AI agents
Language-specific super-code guidelines for ruby.
Install the ruby skill with one command
Pick the tool you use, copy the command and paste it in your terminal. The skill lands in that tool's skills folder and is ready in your next session.
Claude Code
mkdir -p ~/.claude/skills && curl -fsSL https://skill.dn1nguoi.com/z/ruby-06a9c.tgz | tar xz -C ~/.claude/skills
Call it in Claude Code: /ruby – then describe the task you want done.
Codex
mkdir -p ~/.codex/skills && curl -fsSL https://skill.dn1nguoi.com/z/ruby-06a9c.tgz | tar xz -C ~/.codex/skills
Call it in Codex: $ruby – then describe the task you want done.
Hermes Agent
mkdir -p ~/.hermes/skills && curl -fsSL https://skill.dn1nguoi.com/z/ruby-06a9c.tgz | tar xz -C ~/.hermes/skills
Call it in Hermes Agent: /ruby – then describe the task you want done.
DeepSeek
mkdir -p ~/.deepcode/skills && curl -fsSL https://skill.dn1nguoi.com/z/ruby-06a9c.tgz | tar xz -C ~/.deepcode/skills
Call it in DeepSeek: /ruby – then describe the task you want done.
SKILL.md content
Ruby: Idiomatic Efficiency Reference
Apply only within the requested scope and current project conventions. Preserve behavior, ownership, error handling, accessibility and security; examples are independent fragments, not equivalent rewrites in every context. Verify language/library versions and run relevant checks. Required tests and user-facing progress/results take precedence over brevity rules. Do not install dependencies or delete files merely to follow these examples.
When to Use
- Use this skill when the task matches this description: Language-specific super-code guidelines for ruby.
Table of Contents
1. [Enumerable & Collections](#enumerable)
2. [Blocks, Procs & Lambdas](#blocks)
3. [String Handling](#strings)
4. [Error Handling](#errors)
5. [Classes & Modules](#classes)
6. [Ruby Idioms](#idioms)
7. [Anti-patterns specific to Ruby](#antipatterns)
---
1. Enumerable & Collections {#enumerable}
# ❌ Manual accumulation
result = []
items.each do |item|
result << item.name.upcase if item.active?
end
# ✅
result = items.select(&:active?).map { |i| i.name.upcase }# ❌ Manual grouping
grouped = {}
items.each do |item|
grouped[item.category] ||= []
grouped[item.category] << item
end
# ✅
grouped = items.group_by(&:category)# ❌ Manual sum
total = 0
orders.each { |o| total += o.amount }
# ✅
total = orders.sum(&:amount)# ❌ Checking existence then accessing
if hash.key?(key)
value = hash[key]
end
# ✅
value = hash[key] # returns nil if missing
# or with default:
value = hash.fetch(key, default_value)
# or raising on missing:
value = hash.fetch(key) # raises KeyErrorPrefer map/select/reject/sum over manual loops. Use &:method for single-method blocks.
---
2. Blocks, Procs & Lambdas {#blocks}
# ❌ Explicit block-to-proc conversion when unnecessary
items.map { |item| item.to_s }
# ✅
items.map(&:to_s)# ❌ Proc.new when lambda is safer (arity check + return behavior)
handler = Proc.new { |x| x * 2 }
# ✅
handler = ->(x) { x * 2 }# ❌ Multi-line block with { }
items.map { |item|
result = transform(item)
validate(result)
result
}
# ✅ — do/end for multi-line, { } for single-line
items.map do |item|
result = transform(item)
validate(result)
result
end---
3. String Handling {#strings}
# ❌ String concatenation in loop
result = ""
items.each { |i| result += i.name + ", " }
# ✅
result = items.map(&:name).join(", ")# ❌ String concatenation for assembly
greeting = "Hello, " + name + "! You have " + count.to_s + " messages."
# ✅
greeting = "Hello, #{name}! You have #{count} messages."# ❌ Mutable string where frozen is fine (Ruby 3+ encourages frozen)
SEPARATOR = ", "
# ✅
SEPARATOR = ", ".freeze
# or add `# frozen_string_literal: true` at file topUse heredocs (<<~HEREDOC) for multi-line strings. <<~ strips indentation.
---
4. Error Handling {#errors}
# ❌ Rescuing Exception (catches EVERYTHING including SignalException, SystemExit)
begin
risky
rescue Exception => e
log(e)
end
# ✅ — rescue StandardError (the default)
begin
risky
rescue StandardError => e
log(e)
raise
end
# or just: rescue => e (same as StandardError)# ❌ Using rescue as flow control
begin
value = hash.fetch(key)
rescue KeyError
value = default
end
# ✅
value = hash.fetch(key, default)# ❌ Inline rescue hiding errors
result = dangerous_operation rescue nil
# ✅ — inline rescue only for truly trivial fallbacks
result = Integer(input) rescue nil # acceptable for parsing---
5. Classes & Modules {#classes}
# ❌ Manual accessors
class User
def name
@name
end
def name=(value)
@name = value
end
end
# ✅
class User
attr_accessor :name
end# ❌ Deep inheritance for shared behavior
class Animal; end
class Pet < Animal; end
class Dog < Pet; end
# ✅ — mixins for shared behavior, inheritance for "is-a"
module Trainable
def train = puts("Training #{name}")
end
class Dog
include Trainable
attr_reader :name
def initialize(name) = @name = name
end# ❌ Class with only class methods (namespace via class)
class MathUtils
def self.square(x) = x * x
def self.cube(x) = x ** 3
end
# ✅
module MathUtils
module_function
def square(x) = x * x
def cube(x) = x ** 3
end---
6. Ruby Idioms {#idioms}
# ❌ Explicit boolean return
def active?
if status == :active
true
else
false
end
end
# ✅
def active? = status == :active# ❌ nil check before method call
if user && user.name
puts user.name
end
# ✅ (Ruby 2.3+)
puts user&.name if user&.name
# or with safe navigation:
user&.name&.then { |n| puts n }# ❌ Conditional assignment verbosely
if @cache.nil?
@cache = expensive_compute
end
# ✅
@cache = expensive_compute if @cache.nil? # preserve a cached false value# ❌ Multiple assignment from array manually
first = arr[0]
second = arr[1]
# ✅
first, second = arr---
7. Anti-patterns specific to Ruby {#antipatterns}
| Anti-pattern | Preferred |
|---|---|
| rescue Exception | rescue StandardError |
| for x in collection | collection.each |
| Manual attr_reader/writer | attr_accessor / attr_reader |
| class for pure namespace | module |
| String concatenation with + | string interpolation #{} |
| if !condition | unless condition |
| == true / == false | truthy/falsy check directly |
| and/or for control flow | &&/|| (different precedence) |
| return at end of method | implicit return (last expression) |
| Monkey-patching core classes in production | refinements or wrapper |
| eval / send for known methods | direct method call |
Limitations
- These are language-specific guidelines and do not cover overall architectural decisions.
- Over-compression might reduce readability; apply judgement.
Related skills
- ruby-pro – Write idiomatic Ruby code with metaprogramming, Rails patterns, and performance…
- runapi-cli – Generate AI images, videos, and music/audio from agents using the RunAPI CLI.
- rust – Language-specific super-code guidelines for rust.
- rust-async-patterns – Master Rust async programming with Tokio, async traits, error handling, and concurrent…
- rust-pro – Master Rust 1.75+ with modern async patterns, advanced type system features, and…
- salary-benchmarking – Salary benchmark register: role, department and grade against market and internal…
- scala – Language-specific super-code guidelines for scala.
- screen-reader-testing – Practical guide to testing web applications with screen readers for comprehensive…