ruby and 语句_在Ruby中重做和重试语句

ruby and 语句

Ruby重做和重试语句 (Ruby redo and retry statements)

Ruby is a very flexible language; it provides you many ways through which you can make your code more effective and clear. With the help of redo and retry statement, you can make your code more readable to the user as well as you can perform functionalities more efficiently. Let us go through the syntax, area of implementation and example of each of the statements.

Ruby是一种非常灵活的语言。 它为您提供了许多使代码更有效和更清晰的方法。 借助重做和重试语句,可以使代码对用户更易读,并且可以更有效地执行功能。 让我们仔细阅读每个语句的语法,实现区域和示例。

1)Ruby重做语句 (1) Ruby redo statement )

redo statement comes into the scene when you have to re-execute the iteration in the same loop. It repeats the current iteration in the loop. The feature of the redo statement is that it executes without even evaluating the loop condition. Sometimes, this results in the creation of an infinite loop. Let us see the syntax and examples.

当您必须在同一循环中重新执行迭代时, 重做语句就会出现。 它在循环中重复当前的迭代。 重做语句的功能是它执行时甚至不评估循环条件。 有时,这会导致创建无限循环。 让我们看一下语法和示例。

Syntax:

句法:

    while (#condition)
        #statements
        redo if  #condition
    end

Example 1:

范例1:

=begin
Ruby program to demonstrate redo 
=end

i = 30   

while(i < 50)      
    puts "Value of i is #{i}"   
    i += 1   
    redo if i == 50
end   

Output

输出量

Value of i is 30
Value of i is 31
Value of i is 32
Value of i is 33
Value of i is 34
Value of i is 35
Value of i is 36
Value of i is 37
Value of i is 38
Value of i is 39
Value of i is 40
Value of i is 41
Value of i is 42
Value of i is 43
Value of i is 44
Value of i is 45
Value of i is 46
Value of i is 47
Value of i is 48
Value of i is 49
Value of i is 50

Explanation:

说明:

In the above code, you can see that the condition specified in the while loop tells the iteration to be done up to 49 but due to the introduction of redo statement the iteration is going up to 50 because redo statement executes before the condition is checked.

在上面的代码中,您可以看到while循环中指定的条件告诉迭代最多可以完成49次,但是由于引入了redo语句,因此迭代最多可以进行50次,因为redo语句在检查条件之前执行。

Example 2:

范例2:

=begin
Ruby program to demonstrate redo 
=end

i = 30   

while(i < 50)      
    puts "Value of i is #{i}"   
    i += 1   
    redo if i > 45
end   

Output

输出量

An infinite loop

Explanation:

说明:

The above code will give an infinite loop because i will always be greater than 45.

上面的代码将给出一个无限循环,因为我将始终大于45

2)Ruby重试语句 (2) Ruby retry statement)

In Ruby, the retry statement only works inside the begin/rescue block. You can also say that it works in the context of exception handling. You should use a retry statement when you want to re-execute the block.

在Ruby中, retry语句仅在begin / rescue块内起作用。 您也可以说它在异常处理的上下文中起作用。 要重新执行该块时,应使用retry语句

Syntax:

句法:

    retry

Example:

例:

=begin
Ruby program to demonstrate retry 
=end

for i in 0..10
	begin
		puts "VALUE OF i #{i}"
		raise if i >=9
		rescue
		retry
	end
end

Output

输出量

Infinite loop.

Explanation:

说明:

This will also give an infinite loop because exception will be raised until i>=9 and this is an infinite situation.

这也将产生无限循环,因为将引发异常,直到i> = 9 ,这是无限情况。

翻译自: https://www.includehelp.com/ruby/redo-and-retry-statements.aspx

ruby and 语句

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值