home PYTHONJAVA
 

Ruby loop

A loop in Ruby is used to execute the same block of code several times. This section details all of the looping statements supported by Ruby.

Ruby while statement

Syntax

while conditional [do] code end

Or

grammar

while conditional [:] code end

Execute code when conditional is true.

in the grammar do or : can be omitted without writing. However, to write a while expression on a line, you must separate the conditional or program block with do or :.

Instance

#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 while $i < $num do puts("In a loop statement i = #$i" ) $i +=1 end

The output of the above example is:

In the loop statement i = 0
In the loop statement i = 1
In a loop statement i = 2
In the loop statement i = 3
In a loop statement i = 4

Ruby while Modifier

grammar

code while condition orbegin code end while conditional

When conditional is true, execute code.

If the while modifier follows an begin statement without an rescue or ensure clause, code It will be executed before the conditional judgment.

Instance

#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 begin puts("In a loop statement i = #$i" ) $i +=1 end while $i < $num

The output of the above example is:

In the loop statement i = 0
In the loop statement i = 1
In a loop statement i = 2
In the loop statement i = 3
In a loop statement i = 4

Ruby until statement

Syntax

until conditional [do] code end

When conditional is false, execute code.

In the

syntax, do can be omitted and not written. However, if you want to write the until formula on one line, you must separate the conditional or program block with do.

Instance

#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 until $i > $num do puts("In a loop statement i = #$i" ) $i +=1; end

The output of the above example is:

In the loop statement i = 0
In the loop statement i = 1
In a loop statement i = 2
In the loop statement i = 3
In a loop statement i = 4
In the loop statement i = 5

Ruby until Modifier

grammar

code until conditional or begin code end until conditional

Execute code when conditional is false.

If the until modifier follows an begin statement without an rescue or ensure clause, code It will be executed before the conditional judgment.

Instance

#!/usr/bin/ruby # -*- coding: UTF-8 -*- $i = 0 $num = 5 begin puts("In a loop statement i = #$i" ) $i +=1; end until $i > $num

The output of the above example is:

In the loop statement i = 0
In the loop statement i = 1
In a loop statement i = 2
In the loop statement i = 3
In a loop statement i = 4
In the loop statement i = 5

Ruby for Statement

grammar

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

First evaluate the expression to get an object, then execute code once for each element in the expression.

Instance

#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 puts "The value of the local variable #{i}" end

Here, we have defined the range 0..5. The statement for i in 0..5 allows the value of i to be from 0 to 5 (inclusive).

The output of the above example is:

The value of the local variable is 0
The value of the local variable is 1
The value of the local variable is 2
The value of the local variable is 3
The value of the local variable is 4
The value of the local variable is 5
The

for...in loop is almost exactly equivalent to:

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

However, the for loop does not create a new scope for local variables.

In the

syntax, do can be omitted and not written. However, if you want to write a for expression on a line, you must separate the conditional or program block with do.

Instance

#!/usr/bin/ruby # -*- coding: UTF-8 -*- (0..5).each do |i| puts "The value of the local variable #{i}" end

The output of the above example is:

The value of the local variable is 0
The value of the local variable is 1
The value of the local variable is 2
The value of the local variable is 3
The value of the local variable is 4
The value of the local variable is 5

Ruby break statement

Syntax

break

Terminate the innermost loop. If called within a block, terminates the method of the associated block (method returns nil).

Instance

#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 if i > 2 then break end puts "The value of the local variable #{i}" end

The output of the above example is:

The value of the local variable is 0
The value of the local variable is 1
The value of the local variable is 2

Ruby next statement

Syntax

next

Jumps to the next iteration of the loop. If called within a block, the execution of the block is terminated (the yield expression returns nil).

Instance

#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 if i < 2 then next end puts "The value of the local variable #{i}" end

The output of the above example is:

The value of the local variable is 2
The value of the local variable is 3
The value of the local variable is 4
The value of the local variable is 5

Ruby redo statement

Syntax

redo

Restart the iteration of the innermost loop without checking the loop condition. If called within a block, restart yield or call.

Instance

#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 0..5 if i < 2 then puts "The value of the local variable #{i}" redo end end

This will produce the following results and will enter an infinite loop:

The value of the local variable is 0
The value of the local variable is 0
............................

Ruby retry statement

Note: Retry is not supported in loops in versions 1.9 and later.

Syntax

retry

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

begin do_something # thrown exceptions rescue # Handling errors< Span class="hl-code"> retry # Start again with begin end

If retry occurs within an iteration, within a block, or within the body of a for expression, the iterative call is restarted. The iterative parameters are reevaluated.

for i in 1..5 retry if some_condition # Start again with i == 1 end

Instance

#!/usr/bin/ruby # -*- coding: UTF-8 -*- for i in 1..5 retry if i > 2 puts "The value of the local variable #{i}" end

This will produce the following results and will enter an infinite loop:

The value of the local variable is 1
The value of the local variable is 2
The value of the local variable is 1
The value of the local variable is 2
The value of the local variable is 1
The value of the local variable is 2
............................





welookups is optimized for learning.© welookups. 2018 - 2019 All Right Reserved and you agree to have read and accepted our term and condition.