Ruby’s Loops
Posted by Michael Dickens on November 3, 2009
The Ruby programming language has many ways to do what you want. For example, it has no fewer than nine types of loops. Here I have each loop, and how you would express it in C. For some of these, I made up the names. And sometimes I called them loops when they are actually iterators. But I can do that if I want to.
0. Basic loop
Since this loop is just a basic loop, it starts with 0.
loop
expression
end
It will just keep going until the loop is broken out of somehow. In C, it would be written like this:
while (true) {
expression
}
1. While loop
while (statement)
expression
end
This loop is rather simple, and has a very close C equivalent:
while (statement) {
expression
}
2. Until loop
until (statement)
expression
end
This may seem rather useless, since it is very similar to a while loop. But it increases readability, and readability is always good. In C it looks like this:
while (!(statement)) {
expression
}
3. For loop
for var in start..end
expression
end
This is intuitive and understandable. It is very much like the Python for loop. Its C equivalent is this:
for (var = start; var < end; var++) {
expression
}
4. Each loop
(start..end).each { |var|
expression
}
This as well is very intuitive, and represents a different way to do the same thing as #3.
5. Each_index loop
array.each_index { |var|
expression
}
This allows you to quickly go through an array without knowing how long it is. In C, it gets ugly:
for (var = 0; var < sizeof(array)/sizeof(int); var++) {
expression
}
The above C code only works in the array is filled with integers.
6. Times loop
(number).times { |var|
expression
}
This is my favorite one. It’s just so elegant. In C, it would be done like this:
for (var = 0; var < number; var++) {
expression
}
7. Upto loop
(start).upto(end) { |var|
expression
}
Another beautifully intuitive loop. I’ve never used this one since I prefer the for loop, but it’s in the toolbox if you ever want it.
8. Step loop
(start).step(end, size) { |var|
expression
}
This is a simple and once again intuitive loop that is much less intuitive in C.
for (var = start; var < end; var += size) {
expression
}
What a wonderful world is the world of Ruby loops and iterators.
LRFLEW said
C code has an “until” loop, so technically, you can say…
Is the same as…
LRFLEW said
GRAMMER CHECK:
should be
phynnboi said
I think you may be confusing “intuitive” with “something I’m used to.” Show any of those Ruby examples to someone who doesn’t know how to program and I doubt he’ll find it intuitive, let alone understandable. COBOL is intuitive (mostly).
Michael Dickens said
I think that something like
(number).timesis way more intuitive and readable than the C for loop, or even the Python for loop.phynnboi said
Yeah, that’s kind of what I was saying. You think it’s more readable and intuitive, but that’s to be expected since you’re used to reading and understanding Ruby. As someone who already understands it, you’re in a poor position to judge its “intuitiveness.” If it’s really intuitive, people who know nothing about programming should be able to guess what it means without explanation; I doubt many could. Heck, I know quite a bit about programming yet wouldn’t have completely figured out “(number).times { |var| expression }” if you hadn’t explained it in terms that are “intuitive” to me (i.e., in terms of C). Even knowing what it means, I don’t find it very intuitive that, e.g., the name of the looping variable is declared what appears to be inside of the loop body. I haven’t seen that done in any other programming language. Actually, most of the examples you gave look really arcane to me, especially coming from languages where the dot notation is typically used to access parts of structs or objects.
Compare to a COBOL loop:
DISPLAY "Starting to run program" PERFORM 3 TIMES DISPLAY ">>>>This is an in line Perform" END-PERFORM DISPLAY "Finished in line Perform"Even a PHB can understand that.
(I’m not trying to advocate COBOL over Ruby, here; I’m just saying that I don’t see “intuitiveness” as one of Ruby’s strengths.)