Summary

Ruby's container classes group items in convenient ways. We looked at four such classes today.

Arrays and hashes are general-purpose containers that accommodate every kind of object. The contents of an array are arranged sequentially and referred to by index, whereas the values in a hash are not maintained in a predictable order and are referred to by objects called keys.

The others are special-purpose containers. Strings can hold only characters. In many ways, strings act like arrays, but they also have some specialized methods, like upcase, that are just for text. Ranges might not be containers in a strict sense, but they describe increasing sequences of numbers and can be treated basically like arrays.

Exercises

1:Suppose you have ...

Get Sams Teach Yourself Ruby in 21 Days now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.