1

Python Equivalent:

>>> for i in range(0, 9, 3):
>>>    print(i)
0
3
6
9

How can this be replicated in Ruby?

What would be the most Ruby-ish way to preform this operation?

David Greydanus
  • 2,551
  • 1
  • 23
  • 42

1 Answers1

4

You want to use step

(0..9).step(3) do |i|
  puts i
end
yez
  • 2,368
  • 9
  • 15
  • 1
    Not that it is such a big of a problem, but I would recommend using "p" instead of "puts". it basically does the same job, but in my experience(not that big :) :D ), "puts" sometimes hides things like quotes sign and etc. Other than that the answer is correct. – nelfurion Oct 30 '15 at 20:04
  • 2
    Don't use `p` in production code. `p` is `inspect`-like, which doesn't return the same information as `puts` and can return very different output. See http://stackoverflow.com/a/1255362/128421 and http://stackoverflow.com/a/1255362/128421. – the Tin Man Oct 30 '15 at 20:17
  • When "debugging", yes. For production, no. Inspect output can vary wildly from the normal `puts` if `inspect` is implemented for that object. – the Tin Man Oct 30 '15 at 20:22