Ruby on Rails — Day 1: Looping

How many ways to loop in Ruby?

Question: print out “I love Ruby!” 1o times

Solutions:

  1. Using .times

10.times {print “I love Ruby!”}

2. Using for in

for i in 1..10

print “I love Ruby!”

end

3. Using while

i = 10

while i < 11

print “I love Ruby!”

i += 1

end

4. Using loop

i=0

loop do

print “I love Ruby!”

i +=1

break if i==11

end

5. Using until

i = 0

until i == 11 do

print “I love Ruby!”

i +=1

end