Chapter 10. Collections central: Enumerable and Enumerator

 

This chapter covers

  • Mixing Enumerable into your classes
  • The use of Enumerable methods in collection objects
  • Strings as quasi-enumerable objects
  • Sorting enumerables with the Comparable module
  • Enumerators

All collection objects aren’t created equal—but an awful lot of them have many characteristics in common. In Ruby, common characteristics among many objects tend to reside in modules. Collections are no exception: collection objects in Ruby typically include the Enumerable module.

Classes that use Enumerable enter into a kind of contract: the class has to define an instance method called each, and in return, Enumerable endows the objects of the class with all sorts of collection-related behaviors. The methods behind these behaviors are defined in terms of each. In some respects, you might say the whole concept of a “collection” in Ruby is pegged to the Enumerable module and the methods it defines on top of each.

10.1. Gaining enumerability through each

10.2. Enumerable Boolean queries

10.3. Enumerable searching and selecting

10.4. Element-wise enumerable operations

10.5. Relatives of each

10.6. The map method

10.7. Strings as quasi-enumerables

10.8. Sorting enumerables

10.9. Enumerators and the next dimension of enumerability

10.10. Enumerator semantics and uses

10.11. Enumerator method chaining

10.12. Lazy enumerators

Summary

sitemap