Ah, collection classes. What would object-oriented programming be without them? A lot harder. And I mean a whole lot harder. Like you would have to write programs for every instance of every amount of input a user can give you, or you could cripple your code with a lot of overhead by having to create, open, and close files. Or you could get even more creative, but it'd still be harder. Your call.

No worries, though. We have arrays to help out. Arrays are collections in object-oriented languages, like Ruby, that pretty much act as numbered lists. The one-dimensional ones, anyways, which is all we'll be dealing with today. These lists start at 0 and go up to whatever number your heart desires (or whatever number your processor can handle before bursting into flames. Or tears. Or flaming tears. Sweet.)

Let's take a look at one now. First, you instantiate your array with a call like this:


  to_do_list = ['make breakfast', 'laundry', 'increment a number by 1 forever until flaming tears come streaming out from my computer', dishes]

And that's it. They're pretty simple to make. You just give it a name, like to_do_list in our example, and fill the [] brackets with all of the items you want in it. We can print out array and receive a list by doing the following:


  puts to_do_list

And that gets us this:


  make breakfast
  laundry
  increment a number by 1 forever until flaming tears come streaming out from my computer
  dishes

If we want to retrieve a single item from the array, we make a call like so:


  to_do_list[1]

What we're asking our code to do here is to return the value at index 1, which is:


  laundry

Hooray for laundry! Well, for laundry being the vaue retreived from our array!

Simple, right? One-dimensional arrays are perfect for ordered lists. They're not so great for pairs, though...

Let's say you wanted to take some friends on a field trip, and since it's always dangerous to go alone, you're using the buddy system. You make yourself an array of all of your friends and try to group them together by their buddy:


  field_trip_buddies = ['Mario', 'Princess Peach', 'Luigi', 'Ganon', 'Zelda', 'Link', 'Sonic', 'Tails']

That's going to be one amazing field trip. And everyone's next to their buddy! We've got Princess Peach and Mario, and we've got Sonic and Link, and we've got Zelda and Ganon... and Luigi and Princess Peach... wait... this could get awkward. When we're in the middle of our list, we have no idea of knowing who's with whom.

Cue the entrance of our hero, the hash. A hash is a collection of pairs in most object-oriented languages, again like Ruby. To get to know this unlikely hero, let's first make one using our field trip example:


  field_trip_buddies = {'Mario' => 'Princess Peach', 'Luigi' => 'Ganon', 'Zelda' => 'Link', 'Sonic' => 'Tails'}

There. Now everyone should be all snug with their buddy. Well, paired with them, anyways. Now if we want to find out who's paired with Mario, we can make the following call:


  puts field_trip_buddies['Mario']

And we get...


  Princess Peach

No surprises there; we always kind of knew she'd end up with him. And who does Luigi get for his buddy?


  puts field_trip_buddies['Luigi']  


  Ganon

Huh... Well, I've seen stranger ships in fanfics. And it works! Now there's no question about who's holding whose hand as we all file off the bus.

So that about wraps it up. Arrays are great for lists, especially ordered ones. Hashes are better for pairs that don't really need an order. Now if you'll excuse me, I need to get to index 2 on my todo list asap.


def burn_baby_burn
  count_til_you_die = 0

  while true
    count_til_you_die += 1
  end
end

Sweet.