Loops in Ruby are used to perform the same number of times as the code block specifies. This chapter details the looping statements that are supported by Ruby.
Ruby While statement:
Syntax:
While conditional [do]
Code
End
Executes code when the condition is true. The while loop condition is a reserved word in the code, a newline, a backslash (\), or a semicolon separated.
Instance:
#!/usr/bin/ruby
$i = 0
$num = 5 while
$i < $num did
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 loo P i = 4
Ruby while rhetorical character:
Syntax:
Code while condition
OR
Begin
Code
End While conditional
Executes the code when the condition is true.
If the while modifier is immediately following a BEGIN statement but there is no rescue or ensure clause, the code is evaluated before the previous condition.
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
Executes the code when the condition is false. Until a reserved word, a newline character, or a semicolon, separated from code by a conditional statement.
Statement:
#!/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 loo P i = 4
Inside the loop i = 5
Ruby until rhetoric characters:
Syntax:
Code until conditional
OR
Begin
Code
End until conditional
Executes the code when the condition is false.
If the until rhetoric follows the BEGIN statement but there is no rescue or ensure clause, the code is executed before the condition is evaluated.
Example:
#!/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:
Syntax:
For variable [, variable ...] in expression [do]
Code
End
Executes each element of the code at once in an in expression.
Instance:
#!/usr/bin/ruby for
i-0..5
puts "Value of local variable are #{i}"
end
Here we define the range 0. 5. This produces the following results, because in the statement for I in 0..5 will allow the range of values to be taken from 0 to 5 (including 5):
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 cycle is almost entirely equivalent to:
(expression). Each do |variable[, variable...]| Code end
Except that a for loop does not create a range of new local variables. A circular expression is separated from code, reserved words, a newline character, or a semicolon.
Example:
#!/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:
Syntax:
Break
Terminates most internal loops. The method within the terminating block returns nil if the method called is associated with the block.
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 the call block terminates execution within a block (with yield or calls to return nil).
Example:
#!/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:
Syntax:
Redo
Restarts this most internal loop iteration without checking the loop condition.
Will restart yield or call if a block is invoked.
Example:
#!/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 result, which will perform an infinite loop:
The value of local variable is 0 Value of the local variable is 0 ......
.........
Ruby Retry statement:
Syntax:
Retry
If retry expresses the rescue clause now, start over again.
Begin
Do_something # exception raised
Rescue
# handles error
Retry # Restart from beginning
end
If there is a retry iteration, a block, or an expression in the body, restart the iteration call. The parameter condition of the iterator will be recalculated.
For i in 1..5
retry if Some_condition # restart from i = = 1 End
Instance:
#!/usr/bin/ruby for
i-1..5
retry if i > 2
puts "Value of local variable are #{i}"
end
This will produce the following results, which will enter 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 .......
.......