1

I would like to get a range of numbers but only by 5. Is there method for this? Perhaps something like: (0..100).by(5) (I feel like I've seen this done somewhere...)

I know I can do this: (0..100).select{|x| x if x % 5 == 0} Can you suggest alternatives?

MrPizzaFace
  • 7,807
  • 15
  • 79
  • 123

2 Answers2

3

You want #step .

(0..100).step(5)....

Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317
Philip Hallstrom
  • 19,673
  • 2
  • 42
  • 46
3

Look at the method Numeric#step. I am sure it is what, you want.

0.step(25,5).to_a
# => [0, 5, 10, 15, 20, 25]
Arup Rakshit
  • 116,827
  • 30
  • 260
  • 317