Chapter 8
Arrays and Iterators

Welcome back! Let’s write a program that asks us to type in as many words as we want (one word per line, continuing until we just press Enter on an empty line) and then repeats the words back to us in alphabetical order. OK?

So…first we’ll—uh…um…hmmm…. Well, we could—er…em….

You know, I don’t think we can do it. We need a way to store an unknown number of words and to keep track of them altogether so they don’t get mixed up with other variables. We need to put them in some sort of a list. We need arrays.

An array is just a list in your computer. Every slot in the list acts like a variable: you can see what object a particular slot points to, and you can make it point to a different object. Let’s take a look at some arrays:

[]
[5]
[​'Hello'​, ​'Goodbye'​]
flavor = ​'vanilla'​ ​# Not an array, of course...
[89.9, flavor, [true, false]] ​# ...but this is.

First we have an empty array, then an array holding a single number, and then an array holding two strings. Next we have a simple assignment; then we have an array holding three objects, the last of which is the array [true, false]. Remember, variables aren’t objects, so our last array is really pointing to a float, a string, and an array. (Even if we were to set flavor to point to something else later in the program, that wouldn’t change the array.)

To help us find a particular object in an array, each slot is given an index number. Programmers (and most mathematicians) like to start counting from zero, though, so the first slot in the array is slot zero. Here’s how we would reference the objects in an array:

names = [​'Ada'​, ​'Belle'​, ​'Chris'​]
puts names
puts
puts names[0]
puts names[1]
puts names[2]
puts names[3] ​# This is out of range.
Ada
Belle
Chris
Ada
Belle
Chris

So, we see that puts names prints each name in the array names. Then we use puts names[0] to print out the first name in the array and puts names[1] to print the second. I’m sure this seems confusing, but you do get used to it. You just have to really start thinking that counting begins at zero and stop using words such as first and second. If you go out to a five-course meal, don’t talk about the first course; talk about course zero (and in your head, be thinking course[0]). You have five fingers on your right hand, and their numbers are 0, 1, 2, 3, and 4. My wife and I are jugglers. When we juggle six clubs, we are juggling clubs 0--5. In the next few months, we hope to be able to juggle club 6 (and thus be juggling seven clubs between us). You’ll know you have it when you start using the word zeroth.

Finally, we tried puts names[3], just to see what would happen. Were you expecting an error? As we’ve seen in the past, sometimes when you ask your computer a question, it just doesn’t make sense (at least to the computer); that’s when you get an error. Sometimes, however, you can ask a question, and the answer is nothing. What’s in slot three? Nothing. What is names[3]? nil: Ruby’s way of saying “nothing.” nil is a special object that means “not any other object.” And when you puts nil, it prints out nothing. (Just a new line.)

Now, I said the slots in your arrays act like variables. This means you can assign to them as well. If you just had to guess what that code looked like, you’d probably guess something like this:

other_peeps = []
other_peeps[3] = ​'beebee Meaner'
other_peeps[0] = ​'Ah-ha'
other_peeps[1] = ​'Seedee'
other_peeps[0] = ​'beebee Ah-ha'
puts other_peeps
beebee Ah-ha
Seedee
beebee Meaner

As you can see, you don’t have to assign to the slots in any particular order, and any you leave empty are filled with nil by default.

If all this funny numbering of array slots is getting to you, fear not! Often, we can avoid them completely by using various array methods, such as each.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.98.239