Lists

In daily life, lists are all around us. Grocery lists. To-do lists. A dinner recipe has two lists: one for the ingredients, and one for the cooking directions. Your bookshelf is a list of books. There's a stack of plates in your cupboard. An analog clock displays a circularly-arranged list of numbers on its face.

Most of computer programming boils down to managing a bunch of lists. You can see this every day on the internet. Just visit your favorite website and lists will appear like stars in the night sky: the longer you look, the more you'll see.

Consider:

  • The navigation bar at the top of this site is a list of links.
  • This bullet list is, well, a list.
  • Go to your Amazon order history. Yup, it's a list.
  • Google for something. You get a list.
  • Go to your Facebook or Twitter page. Lots of lists.

It behooves us, then, to be comfortable with how to manage lists with Ruby.

Common Traits

All lists in Ruby have three things in common:

  • They have a length
  • They allow you to access a single item from the list
  • They allow you to iterate over each item in the list

We will learn about iteration in section 3.

The following chapters will show how to obtain the length and access items of strings, arrays, and hashes.