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















