O'Reilly logo

Sams Teach Yourself Ruby in 21 Days by Mark Slagell

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

Arrays

An array, or list, is very much like a string in some ways: It is a container whose elements can be referred to by their position and whose elements can be examined and changed using the same techniques you have just learned. But there is a big difference between the two. An array can hold objects of any type, not just characters.

Instead of the quotation marks that you use to create a string, surround a new array with square brackets. Elements of an array are separated by commas.

veggies = ["corn", "carrots"]
small_primes = [2, 3, 5, 7, 11]
some_objects = ["a string", 44, self]

How would you find the second element of the veggies array? Knowing what you know about string objects, you should be able to guess. (Remember, we start counting ...

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