在C和许多其他语言中,有一个continue关键字,当在循环中使用时,它会跳转到循环的下一个迭代。在Ruby中是否有与continue关键字相同的关键字?
当前回答
next
另外,看看重做当前迭代的redo。
其他回答
next
另外,看看重做当前迭代的redo。
是的,它叫next。
for i in 0..5
if i < 2
next
end
puts "Value of local variable is #{i}"
end
输出如下:
Value of local variable is 2
Value of local variable is 3
Value of local variable is 4
Value of local variable is 5
=> 0..5
用一种更地道的方式来写伊恩·伯顿的回答:
(1..5).each do |x|
next if x < 2
puts x
end
打印:
2
3
4
5
使用可以有条件地使用下一个
before = 0
"0;1;2;3".split(";").each.with_index do |now, i|
next if i < 1
puts "before it was #{before}, now it is #{now}"
before = now
end
输出:
before it was 0, now it is 1
before it was 1, now it is 2
before it was 2, now it is 3
使用next,它将绕过该条件,其余代码将正常工作。 下面我提供了完整的脚本和输出
class TestBreak
puts " Enter the nmber"
no= gets.to_i
for i in 1..no
if(i==5)
next
else
puts i
end
end
end
obj=TestBreak.new()
输出: 输入号码 10
1 2 3 4 6 7 8 9 10
推荐文章
- 如何记录Ruby代码?
- Ruby:包含的反义词是什么?Ruby数组?
- 在Ruby中,proc和lambda有什么区别?
- 如何传递参数到一个Rake任务与环境在Rails?
- 获取当前正在执行的方法的名称
- 如何在Rails中计算相对时间?
- 在Ruby中使用范围填充数组的正确方法
- “for”和“each”在Ruby中
- 我如何复制一个哈希在Ruby?
- Ruby/Rails:将Date转换为UNIX时间戳
- 我如何编码/解码HTML实体在Ruby?
- Ruby有一个string. startwith ("abc")内置方法吗?
- 如何从URL下载文件并保存在Rails中?
- 从字符串中移除子字符串
- 如何从Ruby数组中创建平均值?