again and again
Comparing Loops in Ruby and Javascript
Thursday, April 30th, 2015
back to blog index
When I first began studying Ruby, I was familiar with the for loop, as seen in languages like Java. So when I got to the Ruby section on looping, I was expecting more of what I’d already seen. Instead, I was first presented with a notation that I imagine Rubyists are happy to brag about:
5.times do
  puts “Good morning!”
end
I was excited to see that there were options that were significantly more readable than those I had seen before, and I continued joyously on my journey, as I’d grown used to doing with Ruby.
In hindsight, now that I’ve learned more Ruby, and have begun learning JavaScript, I don’t see what I feared. The idea of the most general while loop is a necessary part of any programming language, and Ruby of course had it too. Once I realized its power, I quickly grew comfortable with the syntax and never looked back.
A while loop continues to run while a condition is true. In general, something within the loops effects that condition, eventually causing the loop to stop. Nearly the same loop can be run in both Ruby and JavaScript:
idx = 0
while (idx < 5) [d]
  [print](idx)
  idx += 1
[d]
([d] = delimiter)
Here, the loop is based on an index, which is incremented at the end of each loop. This loop will thus print 0 through 4, then stop, as the index will have reached 5.
Any condition can be used within the parentheses, but this one is so common that JavaScript uses a specific syntax for it: the for loop. Here is the same loop written in that style (I’ll use some specific JS syntax this time):
for (var idx = 0; idx < 5; idx ++)
  console.log(idx);
Many words can be used to describe the form, but it is essentially this: for (start; check; update). The index begins at the value that it’s initialized to in the first portion, it stops as defined in the second portion, and it is updated as given in the final portion.
The for loop in Ruby is NOT the same. It provides the syntax for iteration, and other methods are nearly always used in its place.
Of course, "while" loops with specific conditions are great when the end point of a loop is known or can be easily imagined. When it is not known, an "ongoing" loop can be created:
while (true) {
  [do something]
  break
}
This would continue forever when simply testing “while true,” so an opportunity must be given to end the loop. Both Ruby and Javascript use break. It is generally used in conjunction with a conditional; essentially, “break if…” to end the loop as intended.
Ruby can use this same same notation, or an even simpler alternative is available, just loop.
Instead of ending the loop, both languages also use a keyword that can send the program into the following loop at a given point. Javascript has continue, while Ruby uses next (Ruby also has a few other options). These words add more control, and along with the simple forms we’ve seen, looping provides a lot of power very simply.
Looking back, I’m not sure why the for loop had previously seemed foreign to me. There’s a lot going on between the parentheses for a beginner, but now that I’ve seen many loops, it feels like second nature, and it’s a very compact way to use the general form. Much of the Ruby language is very readable, but at times it provides even more options than are absolutely necessary (and some were skipped here). Used wisely, they can provide a lot of options, but most problems can be solved simply, implementing the standard while loop, or JavaScript’s shortened for form.
back to blog index