O'Reilly logo

Learning Sinatra by Manoj Sehrawat, Sudeep Agarwal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Loops

Ruby has basic loop structures. You will find it very easy to understand them.

while-end

The while-end control structure is executed until the condition is true:

while <condition>
  <statements>
end

The statements are executed until the condition is false.

until-end

This loop structure is the opposite of the while-end structure:

until <condition>
  <statements>
end

The statements are executed until the statement is false.

Fixnum.times

This is similar to the for loop that you might have worked with in other languages:

Fixnum.times do
  <statements>
end

for..in

The for..in loop structure iterates over an object:

for <variable> in <object>
  <statements>
end

Here is an example:

for i in [1, 2, 3, 4, 5]
  puts i
end

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required