liveBook · Manning

Collection

As explained in The Well-Grounded Rubyist, Fourth Edition

A collection is an object that groups multiple elements and supports operations for examining or traversing those elements.

A collection is an object that groups multiple elements so programs can search, sort, filter, count, transform, examine, or traverse them. Ruby’s principal collection classes include Array, Hash, Range, and Set, each emphasizing different organization and behavior. section 9.0 section 9.6

Ruby’s common collection model centers on Enumerable. A class that defines each can include Enumerable and receive shared collection operations; each yields elements to a block one at a time, while more specialized iterators build richer behavior on top. section 10.0 section 6.3.6

Traversing and filtering

Given an array such as [1, 2, 3, 4, 5], each yields each number to a block in order. Enumerable searching methods use similar blocks as selection tests: find returns one matching object, while other methods can return an array of matches or nil. section 6.3.6 section 10.3

class C
  include Enumerable
  def each
    # relevant code here
  end
end

A minimal Enumerable class shows the required each method that unlocks shared collection behavior. section 10.0

Collections need not materialize every intermediate result. An Enumerator is itself an enumerable object that packages iteration separately from a collection and can support chained or lazy processing, such as slicing elements before mapping them. section 10.11.1

How The Well-Grounded Rubyist, Fourth Edition covers it

How deep it goes
Explains major classes, each-based iteration, Enumerable’s contract, filtering results, and Enumerator laziness without exhaustive API coverage.
Examples
Uses array traversal, a custom each implementation, conference payment filtering, numeric selection, and lazy slice-map chains.
What it assumes
Assumes familiarity with objects, methods, arrays, blocks, and basic iteration while reinforcing each and block behavior.
What you can do after
Apply it via each, Enumerable methods, and Enumerator chains to traverse, select, transform, and process grouped objects.
How it connects
Connects arrays, hashes, ranges, sets, Enumerable, Enumerator, each, blocks, and iterators.

Also explained in

Compare how 16 books explain collection →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Rubyist, Fourth Edition. The section links point to the parts of the book it draws on.