Shawn Deprey
Shawn Deprey

Reputation: 525

How would I do this type of for loop in ruby?

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?

Upvotes: 0

Views: 55

Answers (2)

sawa
sawa

Reputation: 168101

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

Upvotes: 1

yonosoytu
yonosoytu

Reputation: 3319

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

You can look at the full documentation.

Upvotes: 2

Related Questions