Tutorial on the usage of loop statements in Ruby and tutorial on the usage of ruby statements

Source: Internet
Author: User

Tutorial on the usage of loop statements in Ruby and tutorial on the usage of ruby statements

The loop in Ruby is used to execute the same code block for a specified number of times. This chapter describes the loop statements supported by Ruby in detail.
Ruby while statement:
Syntax:

While conditional [do]
Code
End

Run the code when the condition is true. The condition for a while loop is that the reserved words in the Code are wrapped in a line break, separated by a backslash (\) or a semicolon.
Instance:

#!/usr/bin/ruby$i = 0$num = 5while $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 rhetoric:
Syntax:

Code while condition

OR

Begin
Code
End while conditional

Run the code when the condition is true.

If the while modifier follows a begin statement but does not have a rescue or ensure clause, the code is evaluated based on the previous condition.
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 = 0Inside the loop i = 1Inside the loop i = 2Inside the loop i = 3Inside the loop i = 4

Ruby until statement:

Until conditional [do]
Code
End

Run the Code if the condition is false. Reserved words separated by the until Condition Statement from the code, line break or semicolon.
Statement:

#!/usr/bin/ruby$i = 0$num = 5until $i > $num do  puts("Inside the loop i = #$i" )  $i +=1;end

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 until rhetoric:
Syntax:

Code until conditional

OR

Begin
Code
End until conditional

Run the Code if the condition is false.

If the until talker follows the begin statement but does not have a rescue or ensure clause, the code is executed before the conditional evaluation.
Example:

#!/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]
Code
End

Each element of code execution is in the in expression.
Instance:

#!/usr/bin/rubyfor i in 0..5  puts "Value of local variable is #{i}"end

The value range is 0 .. 5. Because the range of values allowed in the statement for I in 0 .. 5 is from 0 to 5 (including 5), the following results are generated:

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

Except for a for loop, the range of a new local variable is not created. A circular expression is separated from the code, reserved words, a line break, or a semicolon.
Example:

#!/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 most internal loops. The method in the terminating Block returns nil if the called method and related block.
Instance:

#!/usr/bin/rubyfor 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 most internal loop. If the call block terminates execution within one block (with yield or the call returns nil ).
Example:

#!/usr/bin/rubyfor 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

Starts the innermost loop iteration again without checking the cycle conditions.

Yield or call will be restarted if one block is called.
Example:

#!/usr/bin/rubyfor i in 0..5  if i < 2 then   puts "Value of local variable is #{i}"   redo  endend

This will generate the following results and execute an infinite loop:

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

Ruby retry statement:
Syntax:

Retry

If the retry expression appears in the rescue clause, it starts from the beginning.

begin  do_something # exception raisedrescue  # handles error  retry # restart from beginningend

If retry iteration, block, or body expression occurs, restart the iteration call. The parameter conditions of the iterator are recalculated.

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

Instance:

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

This will generate the following results and will enter 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............................

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.