所以,我有一个ruby循环,如何在其中每50次迭代打印一条消息,如下所示:
loop do
do something
break if something happend
puts "Message at every 50 iteration"
end
答案 0 :(得分:4)
保留一个计数变量,并在每次被50整除时执行某些操作。
i = 0;
loop do
i += 1
if i % 50 == 0
puts "This prints every 50 iterations"
end
end
答案 1 :(得分:4)
i = 50
loop do
# do something
break if something happend # (as in original)
i -= 1
next unless i.zero?
i = 50
puts "Message at every 50 iteration"
end
答案 2 :(得分:0)
考虑到您的条件是c
我认为最干净的方法是使用while
循环(或unless
,具体取决于条件):
while c do
i = (i) ? i + 1 : 1
puts "This prints every 50 iterations" if (i % 50 == 0)
end