我正在尝试在整个数组被遍历之后用ruby重新启动一个数组循环。
我发现retry
方法对此不起作用代码如下:
letters = ["A","B","C"]
letters.each do |letter|
puts letter
if letter == letters.last
puts "that was the last letter"
#restart the array from A again (I'd like it to continue looping infinitely)
end
sleep 1
end
任何想法都将不胜感激谢谢您!
最佳答案
使用cycle
。
letters.cycle do |letter|
...
end
关于arrays - Ruby重新启动数组循环,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35614601/