numb3rs1x numb3rs1x - 6 days ago 7
Ruby Question

serialize ruby fixnum by denominator into an array

I have a number, say it's 1000000. I want to give a second number, say 100 and create an Array that looks like [0,100,200,300,400....1000000]. I'm having trouble finding a way to iterate in a way that would serialize based on a given denominator. Any ideas?

Answer
n = 200
i = 10

(0..n).step(i).to_a
  #=> [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120,
  #    130, 140, 150, 160, 170, 180, 190, 200] 

See Range#step.