Skip to main content

Wuya Tutorial-Ruby - Loop Statement

Wuya Tutorial-Ruby - Loop Statement

Loops in Ruby are used to execute the same block of code a specified number of times. This chapter details all loop statements supported by Ruby.

Ruby while statement

    while conditional [do]
code
end

Execute code when conditional is true. The conditional and code of a while loop are separated by the reserved word do, newline character, backslash\ or semicolon;.

    #!/usr/bin/[ruby](/search?q=ruby)

$i=0
$num=5

while $i < $num do
puts("Inside the loop i=#$i" )
$i +=1
end

This will produce the following 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

If the while modifier follows the begin statement without a rescue or sure clause, the code judgment is performed before the conditional condition is executed.

    #!/usr/bin/ruby

$i=0
$num=5
begin
puts("Inside the loop i=#$i" )
$i +=1
end while $i < $num

This will produce the following 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

Execute code when the condition is false. Conditions up to the statement are separated from code by the reserved word do, a newline character, or a semicolon.

    #!/usr/bin/ruby

$i=0
$num=5

until $i > $num do
puts("Inside the loop i=#$i" )
$i +=1;
end

This will produce the following 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

If the until modifier follows the begin statement without a rescue or sure clause, the code is evaluated conditionally. Executed once before>

    #!/usr/bin/ruby

$i=0
$num=5
begin
puts("Inside the loop i=#$i" )
$i +=1;
end until $i > $num

This will produce the following 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

    for variable [, variable ...] in expression [do]
code
end

Execute code once for each element in the expression.

    #!/usr/bin/ruby

for i in 0..5
puts "Value of local variable is #{i}"
end

Here, [Wuya Tutorial](/search?q=Wuya Tutorial) defines the range 0..5. The statement of i in 0..5 will allow i to accept values ​​in the range 0 to 5 (inclusive). This will produce the following 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

The for ... in loop is almost exactly equivalent to the following -

    (expression).each do |variable[, variable...]| code end

Except that a for loop does not create a new scope for local variables. The expression of a for loop is separated from code by the reserved word do, newline character, or semicolon.

    #!/usr/bin/ruby

(0..5).each do |i|
puts "Value of local variable is #{i}"
end

This will produce the following 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

Ruby Break statement

    break

Terminate the innermost loop. If the related block is called within a block, the method is terminated (the method returns nil ).

    #!/usr/bin/ruby

for i in 0..5
if i > 2 then
break
end
puts "Value of local variable is #{i}"
end

This will produce the following output -

    Value of local variable is 0
Value of local variable is 1
Value of local variable is 2

Ruby next statement

    next

Jump to the next iteration of the innermost loop. If called within a block (either with a yield or a call that returns nil ), terminates execution of that block.

    #!/usr/bin/ruby

for i in 0..5
if i < 2 then
next
end
puts "Value of local variable is #{i}"
end

This will produce the following 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

Ruby redo statement

    redo

If called within a block, restart yield or call .

    #!/usr/bin/ruby

for i in 0..5
if i < 2 then
puts "Value of local variable is #{i}"
redo
end
end

This will produce the following output and will enter an infinite loop -

    Value of local variable is 0
Value of local variable is 0
............................

Ruby retry statement

    retry

If retry appears in the rescue clause of a begin expression, restart from the beginning of the begin body.

    begin
do_something # exception raised
rescue
# handles error
retry # restart from beginning
end

Restarts the iterator call if the retry occurs within the body of an iterator, block, or for expression.

    for i in 1..5
retry if some_condition # restart from i == 1
end

Example

    #!/usr/bin/ruby
for i in 0..5
retry if i > 2
puts "Value of local variable is #{i}"
end

This will produce the following output and will enter an infinite loop -

    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 - Loop Statement - Wuya Tutorial NetworkWuyaTutorial.com provides loops in Ruby for executing the same block of code a specified number of times. This chapter details all loop statements supported by Ruby...Wuya Tutorial-Ruby - Loop Statementhttps://www.learnfk.com/ruby/ruby-loops.html