How to use Loop in Ruby

2020-12-16 hit count image

Let's see how to use the loop like for, white in Ruby

Outline

In Ruby, we can use for, white, and until for the loop. In this blog post, we’ll see what kind of the loops are and how to use them.

Loop

For loop

The For loop will repeat in Array or Range.

for Element in Array or Range do
  Repeated Rctions
end

You can use the For loop like below.

a = [1, 2, 3]

for i in a do
  puts i
end
# 1
# 2
# 3

You can omit do in the For loop like below

a = [1, 2, 3]

for i in a
  puts i
end
# 1
# 2
# 3

Also, you can use the For loop with Range like below.

for i in 1..3 do
  puts i
end
# 1
# 2
# 3

If you want to know more details about Range, see the blog post below.

While loop

The While loop will repeat while a given condition is true.

while Condition do
  Repeated Actions
end

You can use the While loop like below.

i = 1

while i <= 3 do
  puts i
  i += 1
end
# 1
# 2
# 3

You can omit do in the While loop like below.

i = 1

while i <= 3
  puts i
  i += 1
end
# 1
# 2
# 3

Also, you can use the While loop like below.

i = 1

begin
  puts i
  i += 1
end while i <= 3
# 1
# 2
# 3

Util loop

The Until loop will repeat until the given condition is true(while the given condition is false).

until Condition do
  Repeated Actions
end

You can use the Until loop like below.

i = 1

until i > 3 do
  puts i
  i += 1
end
# 1
# 2
# 3

You can omit do in the Until loop like below.

i = 1

until i > 3
  puts i
  i += 1
end
# 1
# 2
# 3

Control the loop

To control the For, While and Until loop, you can use break, next, redo and retry.

Break

If you use the Break in the loop, you can stop the loop.

a = [1, 2, 3]

for i in a do
  puts i
  if i == 2 then
    break
  end
end
# 1
# 2

You can use it in the While loop like below.

i = 1

while i <= 3
  puts i
  i += 1
  if i == 3 then
    break
  end
end
# 1
# 2

You can use it in the Until loop like below.

i = 1

until i > 3
  puts i
  i += 1
  if i == 3 then
    break
  end
end
# 1
# 2

Next

If you use the Next in the loop, you can stop the current loop, and move on the next one.

a = [1, 2, 3]

for i in a do
  if i == 2 then
    next
  end
  puts i
end
# 1
# 3

You can use it in the While loop like below.

i = 0

while i < 3
  i += 1
  if i == 2 then
    next
  end
  puts i
end
# 1
# 3

You can use it in the Until loop like below.

i = 0

until i > 2
  i += 1
  if i == 2 then
    next
  end
  puts i
end
# 1
# 3

Redo

If you use the Redo in the loop, you can stop the current loop, and you can do the loop again with the same condition.

a = [1, 2, 3]

for i in a do
  if i == 2 then
    i = 10
    redo
  end
  puts i
end
# 1
# 10
# 3

You can use it in the While loop like below.

i = 1

while i <= 3
  puts i
  i += 1
  if i == 2 then
    i = 10
    redo
  end
end
# 1
# 10

You can use it in the Until loop like below.

i = 1

until i > 3
  puts i
  i += 1
  if i == 3 then
    i = 10
    redo
  end
end
# 1
# 10

Retry

If the error occurs in the loop, you can start the loop again from the first by using the Retry.

re = 1
a = [1, 2, 3]

begin
  for i in a do
    if re == 3 then
      break
    end
    if i == 3 then
      re += 1
      raise "This is an exception"
    end
    puts i
  end
rescue
  retry
end
# 1
# 2
# 1
# 2

In here, you can see begin...rescue...end. It is the same with try...catch on the other programming languages. If the error occurs in begin...rescue, the codes in rescue...end are executed.

In this example, I used raise to make the error occurs, to use retry.

Completed

We’ve seen how to use the Loop in Ruby and also, we’ve seen how to control the Loop. It’s a little bit hard to understand retry, but it’s a very important loop that restarts the loop from the first when the error occurs, so keep it to remember.

Was my blog helpful? Please leave a comment at the bottom. it will be a great help to me!

App promotion

You can use the applications that are created by this blog writer Deku.
Deku created the applications with Flutter.

If you have interested, please try to download them for free.

Posts