O'Reilly logo

The Book of Ruby by Huw Collingbourne

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 4. Arrays and Hashes

image with no caption

Up to now, you’ve generally been using objects one at a time. In this chapter, you’ll find out how to create a list of objects. You’ll start by looking at the most common type of list structure: an array.

Arrays

An array is a sequential collection of items in which each item can be indexed. In Ruby (unlike many other languages), a single array can contain items of mixed data types such as strings, integers, and floats or even a method call that returns some value. For example, the final element in a1 shown here calls my method, array_length, which returns the length of the array, a0:

array0.rb

def array_length( anArray ) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required