3. Collections

It’s tempting to think of Ruby’s collection classes as containers. If you squint and focus on the two rock stars of the core library, Array and Hash, this is certainly true. But what about the often used Range class? Is it a container? Does having an each method and including the Enumerable module make a class a container?

Alright, I might have stretched that a bit far. You already know that Ruby has several classes which are clearly not containers but use Enumerable. That’s why we throw around the term “collection” which is sometimes synonymous with “container”, but obviously not always.

One of the first classes you usually play around with while learning Ruby is either Array or Hash. That’s because they’re really simple to use, ...

Get Effective Ruby: 48 Specific Ways to Write Better Ruby now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.