我如何在ruby中进行这种类型的for循环?

时间:2022-10-30 08:09:02
for(var i = 0; i < 200000; i += 50){
    // Do Stuff
}

The only way I can think to do it is to do the following

我能想到的唯一方法就是做到以下几点

arr = [0,50,100,...,200000]
arr.each do |i|
    // Do Stuff
end

If that is in fact the only way. How could you build that array quickly?

如果这实际上是唯一的方法。你怎么能快速建立这个阵列?

2 个解决方案

#1


2  

(0..200000).step(50) do |i|
  # Do stuff
end

You can look at the full documentation.

您可以查看完整的文档。

#2


1  

(0...200000).step(50) do |i|
  # Do stuff
end

#1


2  

(0..200000).step(50) do |i|
  # Do stuff
end

You can look at the full documentation.

您可以查看完整的文档。

#2


1  

(0...200000).step(50) do |i|
  # Do stuff
end