Ruby Loops
(Page 1 of 5 )
After last week, I know you've been eagerly waiting for this, the fifth article in my Ruby on Rails series. This one will give you an in-depth look at Loops, Methods, and Blocks. Well maybe Blocks. You'll have to see. So without a bunch of unnecessary yammering, let's get to it.
In the words of the immortal bard, Mr. T: Enough jibba-jabba!
Loops
One day, deep in the forest primeval, I saw a bird with an enormous, striped beak and improbably large eyes. It yelled at me, asserting that I should follow it, as its olfactory sense always knew. What it knew, the bird would not say. But I was curious, so I followed it further into the murks of the forest, unafraid of head hunters, giant spiders, and herculean gorillas. At long last, spent and sweaty, we reached our destination.
A log, with a stupid bowl of sugary rainbow-colored cereal resting atop it. Stupid bird.
Anyway, those loops, while delicious and questionably nutritious, are not the kind of loops we were looking for -- or, more precisely, they're not the kind of loops Ruby uses.
Loops are an exceptional tool for programmers. Loops allow you to repeat the same task over and over again on a set of data. Of course if you aren't careful, you could make the computer run the same set of data infinitely; but we're not amateurs here, so we don't have to worry about that.
You've probably encountered another version of a loop in real life. Like me, on garbage nights, your girlfriend probably gets caught up in one of these loops. In my case, it's as if someone has programmed her to repeatedly remind me that the garbage needs to go to the curb. It's a good thing someone embedded this program in her brain too, because if she didn't loop it over and over, several things would happen: 1) I would still have a full head of lustrous hair, 2) My hands would stop shaking and 3) I would, inevitably, forget to take out the trash.
Next: The While Loop >>
More Ruby-on-Rails Articles
More By James Payne