You've probably noticed that the Ruby collection classes support a
large number of operations that do various things with the
collection: traverse it, sort it, and so on. You may be thinking,
``Gee, it'd sure be nice if
my class could support all these
neat-o features, too!'' (If you actually thought that, it's probably
time to stop watching reruns of 1960s television shows.)
Well, your classes
can support all these neat-o features,
thanks to the magic of mixins and module
Enumerable. All you have
to do is write an iterator called
each, which returns the
elements of your collection in turn. Mix in
Enumerable, and
suddenly your class supports things such as
map,
include?, and
find_all?. If the objects in your collection
implement meaningful ordering semantics using the
<=>
method, you'll also get
min,
max, and
sort.