Ruby Loops
Loops in Ruby are used to execute the same block of code a specified number of times. This section will detail all the loop statements supported by Ruby.
Ruby while
Statement
Syntax
while conditional [do]
code
end
Or
while conditional [:]
code
end
The code
is executed while the conditional
is true.
The do
or :
can be omitted. However, if you want to write the while
statement in one line, you must use do
or :
to separate the conditional from the code block.
Example
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
$i = 0
$num = 5
while $i < $num do
puts("Inside the loop i = #$i" )
$i +=1
end
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 while
Modifier
Syntax
code while condition
Or
begin
code
end while conditional
The code
is executed while the conditional
is true.
If the while
modifier follows a begin
statement with no rescue
or ensure
clauses, the code
is executed once before the conditional
is evaluated.
Example
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
$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
Syntax
until conditional [do]
code
end
The code
is executed while the conditional
is false.
The do
can be omitted. However, if you want to write the until
statement in one line, you must use do
to separate the conditional from the code block.
Example
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
$i = 0
$num = 5
until $i > $num do
puts("Inside the loop i = #$i" )
$i +=1;
end
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
Inside the loop i = 5
Ruby until
Modifier
Syntax
code until conditional
Or
begin
code
end until conditional
The code
is executed while the conditional
is false.
If the until
modifier follows a begin
statement with no rescue
or ensure
clauses, the code
is executed once before the conditional
is evaluated.
Example
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
$i = 0
$num = 5
begin
puts("Inside the loop i = #$i" )
$i +=1;
end until $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
Inside the loop i = 5
Ruby for
Statement
Syntax
for variable [, variable ...] in expression [do]
code
end
The expression is evaluated to get an object, and for each element in the expression
, the code
is executed once.
Example
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
for i in 0..5
puts "The value of local variable is #{i}"
end
Here, we have defined a range 0..5. The statement for i in 0..5
allows i
to take values from 0 to 5 inclusive.
Output:
The value of local variable is 0
The value of local variable is 1
The value of local variable is 2
The value of local variable is 3
The value of local variable is 4
The value of local variable is 5
The for...in
loop is almost completely equivalent to:
(expression).each do |variable[, variable...]| code end
However, the for
loop does not create a new scope for local variables.
The do
can be omitted. However, if you want to write the for
statement in one line, you must use do
to separate the conditional from the code block.
Example
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
(0..5).each do |i|
puts "The value of local variable is #{i}"
end
Output:
The value of local variable is 0
The value of local variable is 1
The value of local variable is 2
The value of local variable is 3
The value of local variable is 4
The value of local variable is 5
Ruby break
Statement
Syntax
break
Terminates the innermost loop. If called within a block, it terminates the associated method (the method returns nil
).
Example
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
for i in 0..5
if i > 2 then
break
end
puts "The value of local variable is #{i}"
end
Output:
The value of local variable is 0
The value of local variable is 1
The value of local variable is 2
Ruby next
Statement
Syntax
next
Jumps to the next iteration of the loop. If called within a block, it terminates the block's execution (yield
expression returns nil
).
Example
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
for i in 0..5
if i < 2 then
next
end
puts "The value of local variable is #{i}"
end
Output:
The value of local variable is 2
The value of local variable is 3
The value of local variable is 4
The value of local variable is 5
Ruby redo
Statement
Syntax
redo
Restarts the current iteration of the innermost loop, without checking the loop condition. If called within a block, it restarts the yield
or call
.
Example
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
for i in 0..5
if i < 2 then
puts "The value of local variable is #{i}"
redo
end
end
This will produce the following result and enter an infinite loop:
The value of local variable is 0
The value of local variable is 0
............................
Ruby retry
Statement
Note: Versions 1.9 and later do not support
retry
in loops.
Syntax
retry
If retry
appears in the rescue
clause of a begin
expression, it restarts from the beginning of the begin
body.
begin
do_something # exception raised
rescue
# handle error
retry # restart from beginning
end
If retry
appears in an iterator, block, or the body of a for
expression, it restarts the invocation of the iterator call. The arguments to the iterator are re-evaluated.
for i in 1..5
retry if some_condition # restart from i == 1
end
Example
#!/usr/bin/ruby
# -*- coding: UTF-8 -*-
for i in 1..5
retry if i > 2
puts "The value of local variable is #{i}"
end
This will produce the following result and enter an infinite loop:
The value of local variable is 1
The value of local variable is 2
The value of local variable is 1
The value of local variable is 2
The value of local variable is 1
The value of local variable is 2
............................