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.
Filter, total, and format a list of sales by chaining Enumerable methods. Run with ruby sales.rb .
📋 Quick Reference — Enumerable
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 }.