Michael Brooks
2/19/2007 4:43:00 PM
Yukihiro Matsumoto wrote:
> Hi,
>
> In message "Re: For loops don't count down"
> on Mon, 19 Feb 2007 22:27:02 +0900, SonOfLilit <sonoflilit@gmail.com> writes:
>
> |Numeric#step counts down:
> |
> |5.step(-1) do |i|
> | #stuff
> |end
>
> 5.downto(1) do |i|
> end
>
> matz.
Hello Yukihiro:
Thank you for the reply. I tried the step, as shown above, and it
doesn't work, it returns a 5, like so:
irb(main):013:0> 5.step(-1) do |i|
irb(main):014:0> puts i
irb(main):015:0> end
=> 5
However, adding the 1 parameter does work, like so:
irb(main):016:0> 5.step(1,-1) do |i|
irb(main):017:0> puts i
irb(main):018:0> end
5
4
3
2
1
=> 5
I'm glad that I'm forced to put in the 1 because I wouldn't want it
counting down to zero without me realizing it.
However, coming from other languages (e.g. Delphi) I still prefer the
for...end syntax because of the more easily recognizable blocks it
creates, like so:
for
...
end
but in time I'm sure I'll get used to seeing number / objects / sets /
ranges at the beginning of lines and won't be so hung up on keywords
like "for".
Maybe if the Range#each were smart enough to look at the start and end
value and count up or down accordingly that would fix things for the
(5..1) style syntax. I assume this wouldn't just benefit the "for" but
would cause less gotcha's wherever ranges were used.
BTW, thank you for creating such a wonderfully OO language. I looked at
Python and it seemed a little inconsistent at times, not to mention all
that __<stuff>__, so I was impressed to find such an rich, clean and OO
oriented language like Ruby. I was equally impressed to see how much
forethought had gone into aspects like sets, iterators, etc. So, please
keep up the great work!
Michael