Advanced Enumerable

Enumerable is the module that gives every Ruby collection its powerful iteration methods, and the advanced ones — reduce , group_by , chunk_while , and friends — let you transform and summarize data in a single expressive line.

Learn Advanced 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.

Building on the each and map you already know, you'll collapse, classify, and reshape collections with the methods professionals reach for daily.

What You'll Learn in This Lesson

1️⃣ reduce / inject — Collapse to One Value

reduce threads an accumulator through every element, returning a single result. Give it a starting value and a block, or a shorthand symbol like :+ for simple operators. Whatever the block returns becomes the accumulator for the next element.

2️⃣ Classifying: group_by, partition, tally

group_by sorts elements into a hash of buckets keyed by the block's result; partition is the two-bucket special case returning [matches, rest] ; and tally counts how many times each element appears.

3️⃣ Building & Ordering: each_with_object, sort_by, chunk_while

each_with_object builds up a mutable container without juggling return values, flat_map maps then flattens one level, the _by methods order by a computed key, and chunk_while groups consecutive runs that satisfy a condition.

Your turn. Fill in each ___ blank with the right method, then run it.

Combine tally , max_by , and group_by to summarize a sentence. Run with ruby wordstats.rb .

📋 Quick Reference — Advanced Enumerable

Practice quiz

What does return?

  • 24

reduce(:+) sums every element: 1+2+3+4 = 10.

What does return?

  • 24
  • 10
  • 0
  • 11

Answer: 24. Starting at 1 and multiplying gives the product 1*1*2*3*4 = 24.

What does group_by return?

  • A two-element array
  • A single value
  • A sorted array
  • A Hash of key => array of matching items

Answer: A Hash of key => array of matching items. group_by builds a Hash mapping each block result to the array of elements that produced it.

partition is the special case of grouping into how many buckets?

  • One

partition returns exactly two arrays: [matches, non_matches] based on a true/false block.

What does return?

  • {"yes"=>2, "no"=>1}
  • {"yes"=>1, "no"=>1}

Answer: {"yes"=>2, "no"=>1}. tally counts occurrences of each element: yes appears twice, no once.

Why prefer each_with_object over reduce when building a Hash?

  • It is faster to type
  • It deeply flattens
  • It passes the same object each time and ignores the block's return
  • It sorts the keys

Answer: It passes the same object each time and ignores the block's return. each_with_object hands you the same object every iteration and ignores the block's return, so you just mutate it.

What does return?

  • The largest age value
  • The whole person with the largest age
  • true/false
  • A sorted array

Answer: The whole person with the largest age. max_by returns the element itself, ordered by the computed key — the whole person, not just the age.

How deeply does flat_map flatten?

  • All levels
  • Two levels
  • It does not flatten
  • Exactly one level

Answer: Exactly one level. flat_map maps then flattens just one level; use flatten for deeper nesting.

What does produce?

chunk_while groups consecutive runs where each next value is one more than the previous.

When building a counter with each_with_object, what does provide?

  • A frozen hash
  • An empty array default
  • A default of 0 for every missing key
  • A KeyError on miss

Answer: A default of 0 for every missing key. Hash.new(0) gives missing keys a default of 0, so memo[key] += 1 works on first sight.