# Map - transform elements
[1, 2, 3, 4].map { |n| n * 2 } # => [2, 4, 6, 8]
['alice', 'bob'].map(&:upcase) # => ['ALICE', 'BOB']
users.map(&:email) # => ['alice@example.com', ...]
# Select/Reject - filter elements
[1, 2, 3, 4, 5].select(&:even?) # => [2, 4]
[1, 2, 3, 4, 5].reject(&:even?) # => [1, 3, 5]
users.select { |u| u.active? }
# Reduce - aggregate values
[1, 2, 3, 4].reduce(:+) # => 10
[1, 2, 3, 4].reduce(0) { |sum, n| sum + n } # => 10
items.reduce(0) { |total, item| total + item.price }
# Find - return first match
[1, 2, 3, 4].find(&:even?) # => 2
users.find { |u| u.email == 'alice@example.com' }
# Group by - partition into hash
users.group_by(&:role)
# => { 'admin' => [<User>, ...], 'user' => [...] }
words = ['apple', 'apricot', 'banana', 'blueberry']
words.group_by { |word| word[0] }
# => { 'a' => ['apple', 'apricot'], 'b' => ['banana', 'blueberry'] }
# Partition - split into two arrays
numbers = [1, 2, 3, 4, 5, 6]
even, odd = numbers.partition(&:even?)
# even => [2, 4, 6], odd => [1, 3, 5]
# Any/All/None - test conditions
[1, 2, 3].any?(&:even?) # => true
[2, 4, 6].all?(&:even?) # => true
[1, 3, 5].none?(&:even?) # => true
# Count - count matching elements
[1, 2, 3, 4].count(&:even?) # => 2
# Tally - count occurrences (Ruby 2.7+)
['a', 'b', 'a', 'c', 'b', 'a'].tally
# => { 'a' => 3, 'b' => 2, 'c' => 1 }
# Sort by multiple criteria
users.sort_by { |u| [u.last_name, u.first_name] }
# Min/Max by criteria
users.min_by(&:age)
users.max_by { |u| u.posts.count }
# Take/Drop
users.take(5) # First 5 users
users.drop(5) # All except first 5
# Each with object - build custom result
(1..5).each_with_object([]) { |n, arr| arr << n * 2 }
# => [2, 4, 6, 8, 10]
# Chunk - group consecutive elements
[1, 1, 2, 2, 2, 3, 1].chunk(&:itself).to_a
# => [[1, [1, 1]], [2, [2, 2, 2]], [3, [3]], [1, [1]]]
# Slice - take elements in groups
[1, 2, 3, 4, 5, 6].each_slice(2).to_a
# => [[1, 2], [3, 4], [5, 6]]
# Zip - combine arrays
names = ['Alice', 'Bob', 'Charlie']
ages = [25, 30, 35]
names.zip(ages)
# => [['Alice', 25], ['Bob', 30], ['Charlie', 35]]
# Flat map - map then flatten
[[1, 2], [3, 4]].flat_map { |arr| arr.map { |n| n * 2 } }
# => [2, 4, 6, 8]
users.flat_map(&:posts) # All posts from all users
# Lazy enumerables - defer execution
(1..Float::INFINITY).lazy
.select(&:even?)
.take(10)
.to_a
# => [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
# Useful for large datasets
User.find_each.lazy
.select(&:active?)
.map(&:email)
.first(100)
# Calculate statistics
def statistics(numbers)
{
count: numbers.count,
sum: numbers.sum,
mean: numbers.sum.to_f / numbers.count,
min: numbers.min,
max: numbers.max,
median: numbers.sort[numbers.count / 2]
}
end
# Group and sum
transactions = [
{ category: 'food', amount: 50 },
{ category: 'transport', amount: 20 },
{ category: 'food', amount: 30 }
]
transactions
.group_by { |t| t[:category] }
.transform_values { |group| group.sum { |t| t[:amount] } }
# => { 'food' => 80, 'transport' => 20 }
# Find duplicates
emails = ['a@example.com', 'b@example.com', 'a@example.com', 'c@example.com']
emails.select { |email| emails.count(email) > 1 }.uniq
# => ['a@example.com']
# Frequency map
words = 'the quick brown fox jumps over the lazy dog'.split
word_freq = words.each_with_object(Hash.new(0)) do |word, hash|
hash[word] += 1
end
# => { 'the' => 2, 'quick' => 1, ... }
# Pipeline transformation
result = User.active
.group_by(&:department)
.transform_values { |users| users.map(&:salary).sum }
.sort_by { |_, total| -total }
.first(5)
.to_h
# Top 5 departments by total salary
Ruby's Enumerable module provides rich collection methods. map transforms elements; select/reject filter. reduce aggregates values. find returns first match; find_all returns all matches. group_by partitions by criteria. partition splits into two arrays. any?, all?, none? test conditions. sort_by orders by criteria. uniq removes duplicates. flatten flattens nested arrays. zip combines arrays. Chaining methods creates expressive pipelines. Lazy enumerables defer computation until needed. Symbol-to-proc (&:method_name) shorthand improves readability. Understanding enumerables enables functional programming style—no explicit loops, declarative data transformation. Mastery of Enumerable is essential for idiomatic Ruby.