Iterators & Enumerable
Ruby is a dynamic, beginner-friendly programming language, and the Enumerable module gives every collection a shared toolbox — map, select, reduce, and more — for looping and transforming data elegantly.
Learn Iterators & Enumerable in our free Ruby course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick reference.
Part of the free Ruby course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.
By the end of this lesson you'll transform with map, filter with select, fold with reduce, and chain these methods like a pro.
What You'll Learn in This Lesson
1️⃣ Transform and Filter: map, select, find
map turns a collection into a new one of the same size. select keeps the elements your block approves; reject drops them; find returns the first match. And because each returns a collection, you can chain them into readable pipelines.
2️⃣ Aggregate: reduce, predicates, grouping
reduce folds many values into one using an accumulator. Predicate methods like all? , any? , and count answer yes/no/how-many questions, while sort_by and group_by reorganise data.
Your turn. Process a list of people (hashes) using select and map . Complete the two TODO blocks, then run it.
Mini-Challenge: A Sales Report
Filter, total, and format a list of sales by chaining Enumerable methods. Run with ruby sales.rb .
Common Errors (and the fix)
- Using each when you meant map — If you build an array inside each , you probably wanted map .
- map returns nil values — If your block ends in puts , every mapped value is nil . End with the value.
- reduce with no initial value on an empty array — Returns nil . Provide a start like reduce(0) .
- select vs find confusion — select returns an array; find returns a single element.
- Forgetting integer division in averages — Use .to_f before dividing to keep decimals.
Pro Tips
- 💡 Symbol-to-proc: names.map(&:upcase) is shorthand for map { |n| n.upcase } .
- 💡 each_with_object: great for building a hash while iterating, without a separate accumulator line.
- 💡 lazy for big data: (1..Float::INFINITY).lazy.select(&:even?).first(5) computes only what you take.
📋 Quick Reference — Enumerable
Concept
Ruby Syntax
Transform
arr.map { |x| x*2 }
Filter
arr.select { |x| ... }
First match
arr.find { |x| ... }
Fold
arr.reduce(:+)
Any / all
arr.any? / arr.all?
Group
arr.group_by { }
Frequently Asked Questions
🎉 Lesson Complete!
- ✅ map transforms; select / reject filter
- ✅ find returns the first match
- ✅ reduce folds a collection into one value
- ✅ all? , any? , group_by , sort_by answer richer questions
- ✅ Chaining builds readable data pipelines
- ✅ Next lesson: Classes & Objects — model real-world things
Practice quiz
What does return?
- 6
map transforms each element, returning a new array of the same size: [2, 4, 6].
What does return?
select keeps elements where the block is true — the even numbers [2, 4].
How does reject differ from select?
- It is identical
- It returns a single element
- It sorts the result
- It keeps elements where the block is FALSE
Answer: It keeps elements where the block is FALSE. reject is the mirror of select: it keeps elements for which the block returns false.
What does (alias detect) return?
- All matching elements
- The first matching element (or nil)
- true or false
- A count
Answer: The first matching element (or nil). find returns just the first element matching the block, or nil if none match.
What does return?
- 15
- 120
Answer: 15. reduce(:+) folds the collection into one value by summing: 15.
Which method returns true only if EVERY element passes the block?
- any?
- count
- all?
- find
Answer: all?. all? returns true only when every element satisfies the block; any? needs just one.
What does demonstrate?
- A syntax error
- Chaining Enumerable methods
- Reducing to one value
- Sorting
Answer: Chaining Enumerable methods. Because each method returns a collection, you can chain select then map into a pipeline.
If your map block ends in , what do the mapped values become?
- The printed strings
- true
- An error
- nil for every element
Answer: nil for every element. puts returns nil, so a block ending in puts maps every element to nil. End the block with the value.
What is the Enumerable module?
- A class for numbers only
- A mixin giving collection methods to any class defining each
- A gem you install
- A type of loop
Answer: A mixin giving collection methods to any class defining each. Enumerable is a mixin that provides map, select, reduce, etc. to any class that defines each.
What does use as shorthand?
- A range
- A ternary
- Symbol-to-proc for { |n| n.upcase }
- A lambda literal
Answer: Symbol-to-proc for { |n| n.upcase }. &:upcase is the symbol-to-proc shorthand for { |n| n.upcase }.
Continue this course
- Previous: Procs & Lambdas
- Next: Classes & Objects