| name | ruby |
| description | Language-specific super-code guidelines for ruby. |
| risk | safe |
| source | community |
| date_added | 2026-06-16 |
Ruby: Idiomatic Efficiency Reference
Table of Contents
- Enumerable & Collections
- Blocks, Procs & Lambdas
- String Handling
- Error Handling
- Classes & Modules
- Ruby Idioms
- Anti-patterns specific to Ruby
1. Enumerable & Collections {#enumerable}
result = []
items.each do |item|
result << item.name.upcase if item.active?
end
result = items.select(&:active?).map { |i| i.name.upcase }
grouped = {}
items.each do |item|
grouped[item.category] ||= []
grouped[item.category] << item
end
grouped = items.group_by(&:category)
total = 0
orders.each { |o| total += o.amount }
total = orders.sum(&:amount)
if hash.key?(key)
value = hash[key]
end
value = hash[key]
value = hash.fetch(key, default_value)
value = hash.fetch(key)
Prefer map/select/reject/sum over manual loops. Use &:method for single-method blocks.
2. Blocks, Procs & Lambdas {#blocks}
items.map { |item| item.to_s }
items.map(&:to_s)
handler = Proc.new { |x| x * 2 }
handler = ->(x) { x * 2 }
items.map { |item|
result = transform(item)
validate(result)
result
}
items.map do |item|
result = transform(item)
validate(result)
result
end
3. String Handling {#strings}
result = ""
items.each { |i| result += i.name + ", " }
result = items.map(&:name).join(", ")
greeting = "Hello, " + name + "! You have " + count.to_s + " messages."
greeting = "Hello, #{name}! You have #{count} messages."
SEPARATOR = ", "
SEPARATOR = ", ".freeze
Use heredocs (<<~HEREDOC) for multi-line strings. <<~ strips indentation.
4. Error Handling {#errors}
begin
risky
rescue Exception => e
log(e)
end
begin
risky
rescue StandardError => e
log(e)
raise
end
begin
value = hash.fetch(key)
rescue KeyError
value = default
end
value = hash.fetch(key, default)
result = dangerous_operation rescue nil
result = Integer(input) rescue nil
5. Classes & Modules {#classes}
class User
def name
@name
end
def name=(value)
@name = value
end
end
class User
attr_accessor :name
end
class Animal; end
class Pet < Animal; end
class Dog < Pet; end
module Trainable
def train = puts("Training #{name}")
end
class Dog
include Trainable
attr_reader :name
def initialize(name) = @name = name
end
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}
def active?
if status == :active
true
else
false
end
end
def active? = status == :active
if user && user.name
puts user.name
end
puts user&.name if user&.name
user&.name&.then { |n| puts n }
if @cache.nil?
@cache = expensive_compute
end
@cache ||= expensive_compute
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 | &&/` |
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.