Loops in Ruby are used to execute the same code block several times. This section details all the looping statements that are supported by Ruby.
Ruby While statement
Grammar
While conditional [does]
code
end
When conditional is true, code is executed. The while loop is conditional by preserving the word do, a newline character, a backslash, or a semicolon; , to separate from the code.
Instance
#!/usr/bin/ruby
$i = 0
$num = 5 while
$i < $num do
puts ("Inside the loop i = # $i")
$i +=1
en D
This will produce the following results:
Inside the Loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loo P i = 4
Ruby while modifier
Grammar
Code while condition
OR
begin the code end while
conditional
When conditional is true, code is executed.
If the while modifier follows a BEGIN statement that does not have a rescue or ensure clause, the code executes once before the conditional is judged.
Instance
#!/usr/bin/ruby
$i = 0
$num = 5
begin
puts ("Inside the loop i = # $i")
$i +=1 End While
$i < $n Um
This will produce the following results:
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
When conditional is false, code is executed. The conditional of the until statement is separated from the code by preserving the word do, a newline character, or a semicolon.
Instance
#!/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 results:
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
u Ntil Conditional
When conditional is false, code is executed.
If the until modifier follows a BEGIN statement that does not have a rescue or ensure clause, the code executes once before the conditional is judged.
Instance
#!/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 results:
Inside the Loop i = 0
Inside the loop i = 1
Inside the loop i = 2
Inside the loop i = 3
Inside the loo P i = 4
Inside the loop i = 5
Ruby for statement
Grammar
For variable [, variable ...] in expression [does] code end
Execute code once for each element in the expression.
Instance
#!/usr/bin/ruby for
i-0..5
puts "Value of local variable are #{i}"
end
Here we have defined the scope of the 0..5. The statement for I in 0..5 allows the value of I from 0 to 5 (containing 5). This will produce the following results:
Value of local variable is 0 value of the local variable is 1 Value of the local
variable is 2 value of the local
variable is 3 value of the local variable is 4 Value of the local
variable is 5
The for...in loop is almost entirely equivalent to:
(expression). Each do |variable[, variable...]| Code end
However, the For loop does not create a new scope for local variables. The expression of the For loop is separated from the code by preserving the word do, a newline character, or a semicolon.
Instance
#!/usr/bin/ruby
(0..5). Each do |i|
Puts "Value of local variable are #{i}" end
This will produce the following results:
Value of local variable is 0 value of the local variable is 1 Value of the local
variable is 2 value of the local
variable is 3 value of the local variable is 4 Value of the local
variable is 5
Ruby Break statement
Grammar
Break
Terminates the most internal loop. If called within a block, the method that terminates the associated block (the method returns nil).
Instance
#!/usr/bin/ruby for
i-0..5
if i > 2 then break end
puts ' Value of local variable is #{i} '
end
This will produce the following results:
Value of local variable is 0 value of the local variable is 1 Value of the local
variable is 2
Ruby Next Statement
Grammar
Next
Jumps to the next iteration of the inner loop. If called within a block, the execution of the block is terminated (yield or call returns nil).
Instance
#!/usr/bin/ruby for
i-0..5
if I < 2 then
next end
puts ' Value of local ' variable is
#{i} '
end
This will produce the following results:
Value of local variable is 2 value of the local variable is 3 Value of the local
variable is 4 value of the local
Variab Le is 5
Ruby Redo Statement
Grammar
Redo
Restart the iteration of the most internal loop and do not check the loop condition. If called within a block, restart yield or call.
Instance
#!/usr/bin/ruby for
i-0..5
if I < 2 then
puts "Value of local variable is #{i}"
Redo
0/>end
This produces the following results and goes into an infinite loop:
The value of local variable is 0 Value of the local variable is 0 ......
.........
Ruby Retry Statement
Grammar
Retry
If retry appears in the rescue clause of the BEGIN expression, it starts again from the beginning of the begin body.
the exception thrown by begin Do_something #
Rescue
# processing error
Retry # re-start from begin
If retry appears within an iteration, within a block, or within the body of a for expression, the iteration call is restarted. The parameters of the iteration are reassessed.
For i in 1..5
retry if Some_condition # re-start from i = = 1
Instance
#!/usr/bin/ruby for
i-1..5
retry if i > 2
puts "Value of local variable are #{i}"
end
This produces the following results and goes into an infinite loop:
Value of local variable is 1 value of the local variable is 2 Value of the local
variable is 1 value of the local
Varia BLE is 2 value of the local variable is 1 Value of the local variable is 2 .......
.......