you can use ‘for’
for example, but:10.times do something end
is somehow so clear. It’s almost zenlike.
No, he can’t. If you read his code carefully, he is using step() to count in
steps of 45.
Both for and times() are useless for that, as both always count in ones (also
times does not allow you to specify the beginning/end of the range, either,
just the number of times).
There is another way of using step which I think does read a tad more clear
than the ugly Integer method, which is using a Range object.
Thus, instead of the completely awkward:
0.step(360,45) {}
you write (also watch out for the … / … of range being including/excluding
last number):
(0…360).step(45) {}
The above will do the same as the OP’s original command, but reads more natural
to me (at least I don’t have to consult the docs to figure out whether 360 is
the step or the last value – something I would have to if I was dealing with
variables instead of numbers).
I am not sure if there is a performance difference, thou, as a temp range
object needs to be constructed.
Also, I suspect that, for the example, he really wanted … in the range for
dealing with angles as you don’t want to consider 360.
That being said, I admit I also find that syntax somewhat confusing too and
would rather have a “step” in a for loop or the ability to loop thru ranges (or
arrays) with a step value, like:
for i in (0…360) step 45
end
as you can in python when you do:
for i in range(0,360,45):
This is also needed in my opinion because the other commands open a new block,
which I undestand is a tad less efficient (and it also changes scope rules a
little).
Other than that, you can use a while loop at the cost of typing much more.
On the positive side, ruby’s ranges are still more practical than doing the
same with Perl, where a for or while loop are more or less your only choices.
And unlike python, you can also change the loop condition to be considered at
the end of the loop like C’s do/while() constructs, which was a construct I
tended to miss in python.