Collections: Arrays, Hashes and Enumerable
How to Perform List Operations in Ruby
List operations are at the heart of many programming languages, especially Lisp-based or inspired languages. If you first learned to program on such a language, you see all problems as a list, and solving list problems without list operations is going to be difficult. So if operations like car and cdr are your forte, Ruby has you covered.
Enumerable: Picking Out Just What You Want
The Enumerable mixin provides a collection interface to all manner of collections in Ruby. A collection is just what it sounds like, any object that holds a collection of other objects. Common collections in Ruby are the Array and Hash, but anything that holds objects and implements the each method to iterate over them can use the Enumerable mixin.
Enumerable: Do Any or All Objects Match?
Another useful thing to do with collections is see if any or all of the elements match a certain criteria. For example, if I’m gathering temperature data, I want to sound an alarm if any temperature in an array is over 100 degrees. I want to shut down the system I’m monitoring if all of the temperatures in the sample are over 100 degrees.