Explain the usage of loop statements in Ruby, and explain the usage of ruby statements.
The loop in Ruby is used to execute the same code block several times. This section describes all loop statements supported by Ruby in detail.
Ruby while statement
Syntax
while conditional [do] codeend
When conditional is true, run the code. While loop conditional by reserved words do, a line break, backslash \ or a semicolon;, to separate the code.
Instance
#!/usr/bin/ruby $i = 0$num = 5 while $i < $num do puts("Inside the loop i = #$i" ) $i +=1end
This produces the following results:
Inside the loop i = 0Inside the loop i = 1Inside the loop i = 2Inside the loop i = 3Inside the loop i = 4
Ruby while Modifier
Syntax
code while condition OR begin codeend while conditional
When conditional is true, run the code.
If the while modifier is followed by a begin statement without a rescue or ensure clause, the code will be executed once before the conditional judgment.
Instance
#!/usr/bin/ruby $i = 0$num = 5begin puts("Inside the loop i = #$i" ) $i +=1end while $i < $num
This produces 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] codeend
When conditional is false, run the code. The conditional of the until statement is separated from the code by the reserved word do, a line break, 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 produces 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 codeend until conditional
When conditional is false, run the code.
If the until modifier is followed by a begin statement without a rescue or ensure clause, the code will be executed once before the conditional judgment.
Instance
#!/usr/bin/ruby $i = 0$num = 5begin puts("Inside the loop i = #$i" ) $i +=1;end until $i > $num
This produces the following results:
Inside the loop i = 0Inside the loop i = 1Inside the loop i = 2Inside the loop i = 3Inside the loop i = 4Inside the loop i = 5
Ruby for statement
Syntax
for variable [, variable ...] in expression [do] codeend
Execute code once for each element in expression.
Instance
#!/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 allows the I value to ranges from 0 to 5 (including 5 ). This produces the following results:
Value of local variable is 0Value of local variable is 1Value of local variable is 2Value of local variable is 3Value of local variable is 4Value of local variable is 5
The for... in loop is almost equivalent:
(Expression). each do | variable [, variable...] | code end
However, a for loop does not create a new scope for a local variable. The expression of the for Loop is separated from the code by the reserved word do, a linefeed, or a semicolon ..
Instance
#!/usr/bin/ruby (0..5).each do |i| puts "Value of local variable is #{i}"end
This produces the following results:
Value of local variable is 0Value of local variable is 1Value of local variable is 2Value of local variable is 3Value of local variable is 4Value of local variable is 5
Ruby break statement
Syntax
Break
Terminate the inmost cycle. If it is called within the block, the method of the relevant block is terminated (the method returns nil ).
Instance
#!/usr/bin/ruby for i in 0..5 if i > 2 then break end puts "Value of local variable is #{i}"end
This produces the following results:
Value of local variable is 0Value of local variable is 1Value of local variable is 2
Ruby next statement
Syntax
Next
Jump to the next iteration of the innermost loop. If the block is called, the execution of the block is terminated (yield or nil is returned ).
Instance
#!/usr/bin/ruby for i in 0..5 if i < 2 then next end puts "Value of local variable is #{i}"end
This produces the following results:
Value of local variable is 2Value of local variable is 3Value of local variable is 4Value of local variable is 5
Ruby redo statement
Syntax
Redo
Re-start the iteration of the most internal loop without checking the cycle conditions. If it is called within the block, Restart yield or call.
Instance
#!/usr/bin/ruby for i in 0..5 if i < 2 then puts "Value of local variable is #{i}" redo endend
This produces the following results and enters an infinite loop:
Value of local variable is 0Value of local variable is 0............................
Ruby retry statement
Syntax
Retry
If retry appears in the rescue clause of the begin expression, it starts from the beginning of the in body.
Begin do_something # thrown exception rescue # handle error retry # Start from begin again
If retry appears in an iteration, within a block, or within the body of a for expression, re-start the iteration call. Iteration parameters are re-evaluated.
For I in 1 .. 5 retry if some_condition # Start from I = 1 again
Instance
#!/usr/bin/ruby for i in 1..5 retry if i > 2 puts "Value of local variable is #{i}"end
This produces the following results and enters an infinite loop:
Value of local variable is 1Value of local variable is 2Value of local variable is 1Value of local variable is 2Value of local variable is 1Value of local variable is 2............................