Ruby on rails 跳转到外循环的下一个迭代
我有以下代码:Ruby on rails 跳转到外循环的下一个迭代,ruby-on-rails,ruby,loops,ruby-on-rails-4,Ruby On Rails,Ruby,Loops,Ruby On Rails 4,我有以下代码: quantity.times do ids.each_with_index do |id, index| #a bunch of nested if/thens if index == 0 do something else send termination email end end end 基本上,如果发送终止电子邮件,我只希望发送一次,而不管ids数组中有多少项。发送完后,我想转到数量时间循环中的下一个数字。当然,如
quantity.times do
ids.each_with_index do |id, index|
#a bunch of nested if/thens
if index == 0
do something else
send termination email
end
end
end
基本上,如果发送终止电子邮件,我只希望发送一次,而不管ids
数组中有多少项。发送完后,我想转到数量时间
循环中的下一个数字。当然,如果没有发送终止电子邮件,并且执行了其他操作,则可以继续在ids
数组中循环
我现在使用的是index==0
来触发电子邮件一次,但我想进一步简化
我知道
next
方法,但我知道它会跳转到内部循环的下一次迭代,在本例中是id。每个循环,这不是我想要的。可能您正在寻找break关键字
for i in 0..5
if i > 2 then
break
end
puts "Value of local variable is #{i}"
end
这将产生以下结果:
Value of local variable is 0
Value of local variable is 1
Value of local variable is 2
您可能正在查找break关键字
for i in 0..5
if i > 2 then
break
end
puts "Value of local variable is #{i}"
end
这将产生以下结果:
Value of local variable is 0
Value of local variable is 1
Value of local variable is 2
希望能有帮助
希望有帮助。这可能有点多余,但为了更好地理解你的问题,我做了:
quantity = 2
ids = ["a", "b"]
quantity.times do
puts "outer loop"
ids.each_with_index do |id, index|
puts "inner loop"
# a bunch of nested if/thens
if index == 0
puts "do something else"
puts "send termination email"
break
end
end
end
结果是:
outer loop
inner loop
do something else
send termination email
outer loop
inner loop
do something else
send termination email
如果我取消中断,输出将更改为:
outer loop
inner loop
do something else
send termination email
inner loop
outer loop
inner loop
do something else
send termination email
inner loop
这有效地节省了一些内部循环迭代。这就是你想要的吗?这可能有点多余,但为了更好地理解你的问题,我做了:
quantity = 2
ids = ["a", "b"]
quantity.times do
puts "outer loop"
ids.each_with_index do |id, index|
puts "inner loop"
# a bunch of nested if/thens
if index == 0
puts "do something else"
puts "send termination email"
break
end
end
end
结果是:
outer loop
inner loop
do something else
send termination email
outer loop
inner loop
do something else
send termination email
如果我取消中断,输出将更改为:
outer loop
inner loop
do something else
send termination email
inner loop
outer loop
inner loop
do something else
send termination email
inner loop
这有效地节省了一些内部循环迭代。这就是你要找的吗?很抱歉,太密集了,现在工作可能太晚了。。。但不管怎样,如果我需要做些别的事情,然后发送终止电子邮件,然后中断,那该怎么办呢?我试着这样写代码:1)final if/then语句的计算结果为true
,2)执行其他操作
,3)发送终止电子邮件
,4)中断
。但这打破了我所有的测试…哦,实际上我知道问题是什么。问题在于断点的位置。使用索引==0
,我可以确保if
语句中的整个代码块只发生一次。你知道怎么解决吗?我几乎想在……之后休息一下。。。结束
我的代码完全符合您的要求。执行break
之前的一组代码,然后我们尝试发送电子邮件并中断循环(如果且仅当它成功发送时)。我误解你了吗?是的,解释得不好。基本上,我只想在需要终止电子邮件的迭代中“做些别的事情”,而不是之后的那些。这就是为什么在索引
循环中包装中断仍然很重要的原因抱歉,太密集了,现在开始工作可能已经太晚了。。。但不管怎样,如果我需要做些别的事情,然后发送终止电子邮件,然后中断,那该怎么办呢?我试着这样写代码:1)final if/then语句的计算结果为true
,2)执行其他操作
,3)发送终止电子邮件
,4)中断
。但这打破了我所有的测试…哦,实际上我知道问题是什么。问题在于断点的位置。使用索引==0
,我可以确保if
语句中的整个代码块只发生一次。你知道怎么解决吗?我几乎想在……之后休息一下。。。结束
我的代码完全符合您的要求。执行break
之前的一组代码,然后我们尝试发送电子邮件并中断循环(如果且仅当它成功发送时)。我误解你了吗?是的,解释得不好。基本上,我只想在需要终止电子邮件的迭代中“做些别的事情”,而不是之后的那些。这就是为什么在索引
循环中包装中断仍然很重要的原因是!这是伟大的,我曾想过尝试使用break删除索引,但根据我上面的评论,我明白为什么它是关键的。谢谢对这是伟大的,我曾想过尝试使用break删除索引,但根据我上面的评论,我明白为什么它是关键的。谢谢