1. Technology

Collections: Arrays, Hashes and Enumerable

Most languages have the concept of a "collection," or objects that hold other objects. In Ruby, the basic collections are Arrays (a list of elements) and a Hash (a map of keys to values). While they don't share a common ancestor in the class hierarchy, they both include the Enumerable module, and all share a similar API.

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.

You can opt-out at any time. Please refer to our privacy policy for contact information.

©2014 About.com. All rights reserved.