Ruby - Loops
Loops in Ruby are used to execute the same block of code a specified number of times. This chapter details all the loop statements supported by Ruby.
Syntax
while conditional [do]
code
end
|
Executes code while conditional is true. A while loop's conditional is separated from code by the reserved word do, a newline, backslash \, or a semicolon ;.
#!/usr/bin/ruby
$i = 0 while $i < $num do |
Output
Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loop i = 4
|
code while condition
OR
begin
code
end while conditional
|
Executes code while conditional is true.
If a while modifier follows a begin statement with no rescue or ensure clauses, code is executed once before conditional is evaluated.
#!/usr/bin/ruby
$i = 0
$num = 5
begin
puts("Inside the loop i = #$i" )
$i +=1
end while $i < $num
|
Output
Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loop i = 4
|
Ruby until Statement
until conditional [do]
code
end
|
Executes code while conditional is false. An until statement's conditional is separated from code by the reserved word do, a newline, or a semicolon.
#!/usr/bin/ruby
$i = 0
$num = 5
until $i > $num do
puts("Inside the loop i = #$i" )
$i +=1;
end
|
Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loop i = 4
Inside the loop i = 5
|
code until conditional
OR
begin
code
end until conditional
|
Executes code while conditional is false.
If an until modifier follows a begin statement with no rescue or ensure clauses, code is executed once before conditional is evaluated.
#!/usr/bin/ruby
$i = 0
$num = 5
begin
puts("Inside the loop i = #$i" )
$i +=1;
end until $i > $num
|
Inside the loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loop i = 4
Inside the loop i = 5
|
for variable [, variable ...] in expression [do]
code
end
|
Executes code once for each element in expression.
#!/usr/bin/ruby
for i in 0..5
puts "Value of local variable is #{i}"
end
|
Here, we have defined the range 0..5. The statement for i in 0..5 will allow i to take values in the range from 0 to 5 (including 5). This will produce the following result −
Value of local variable is 0
Value of local variable is 1
Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5
|
A for...in loop is almost exactly equivalent to the following
(expression).each do |variable[, variable...]| code end
|
except that a for loop doesn't create a new scope for local variables. A for loop's expression is separated from code by the reserved word do, a newline, or a semicolon.
#!/usr/bin/ruby
(0..5).each do |i|
puts "Value of local variable is #{i}"
end
|
Output
Value of local variable is 0
Value of local variable is 1
Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5
|
break |
Terminates the most internal loop. Terminates a method with an associated block if called within the block (with the method returning nil).
#!/usr/bin/ruby
for i in 0..5
if i > 2 then
break
end
puts "Value of local variable is #{i}"
end
|
Output
Value of local variable is 0
Value of local variable is 1
Value of local variable is 2
|
next |
Jumps to the next iteration of the most internal loop. Terminates execution of a block if called within a block (with yield or call returning nil).
#!/usr/bin/ruby
for i in 0..5
if i < 2 then
next
end
puts "Value of local variable is #{i}"
end
|
Output
Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5
|
redo |
Restarts this iteration of the most internal loop, without checking loop condition. Restarts yield or call if called within a block.
#!/usr/bin/ruby
for i in 0..5
if i < 2 then
puts "Value of local variable is #{i}"
redo
end
end
|
Output
Value of local variable is 0
Value of local variable is 0
............................
|
Ruby retry Statement
retry |
If retry appears in rescue clause of begin expression, restart from the beginning of the begin body.
begin
do_something # exception raised
rescue
# handles error
retry # restart from beginning
end
|
If retry appears in the iterator, the block, or the body of the for expression, restarts the invocation of the iterator call. Arguments to the iterator is re-evaluated.
for i in 1..5
retry if some_condition # restart from i == 1
end
|
#!/usr/bin/ruby
for i in 0..5
retry if i > 2
puts "Value of local variable is #{i}"
end
|
Output
Value of local variable is 1
Value of local variable is 2
Value of local variable is 1
Value of local variable is 2
Value of local variable is 1
Value of local variable is 2
............................
|
Ruby program to add two integer numbers
how to create an array with Array.[](*args) in Ruby ?
What are the various Ruby runtimes, and how are they different?
Ruby program to check whether the given number is prime or not
Ruby program to reverse a string
Ruby program to check whether the given number is palindrome
Ruby program to print Fibonacci series
How to Replace array elements in Ruby?
Ruby program to print an array
Ruby program to check whether the given number is Armstrong
Program to Print Triangle of Numbers in Ruby
How to add/remove elements to Array in Ruby?
How to shuffle an array in Ruby?
Creating Array with Array.new(size, obj) in Ruby
Ruby program to generate random numbers
Ruby program to Calculate the factorial of given number
What are #method_missing and #send? Why are they useful?
How to Sort Array in Ruby?
How to get index of array element in Ruby
How to Get Input with Gets in Ruby
How to create two dimensional array in ruby?