M
Michael Brooks
Hello:
I just started learning Ruby. I like it's OOness however I've run
across an unusual quirk with the "for loop" that. The "for loop" can
count up through a series of numbers, like so:
for number in (1..5)
puts number.to_s
end
which will output the digits 1 through 5. However, the "for loop" does
not appear to be able to count down when the range start and end is
reversed, like so:
for number in (5..1)
puts number.to_s
end
which outputs nothing. I realize that using "downto" can get me what I
want for down counting, like so:
5.downto(1) do |number|
puts number.to_s
end
but I'd like to use the for because it's easier for me to see the start
and end of the block (especially when using syntax highlighting editors
like RDE).
Can anyone help be understand why the "for loop" can't count down or if
there is an alternative "for loop" syntax that I'm missing.
Thank You,
Michael
I just started learning Ruby. I like it's OOness however I've run
across an unusual quirk with the "for loop" that. The "for loop" can
count up through a series of numbers, like so:
for number in (1..5)
puts number.to_s
end
which will output the digits 1 through 5. However, the "for loop" does
not appear to be able to count down when the range start and end is
reversed, like so:
for number in (5..1)
puts number.to_s
end
which outputs nothing. I realize that using "downto" can get me what I
want for down counting, like so:
5.downto(1) do |number|
puts number.to_s
end
but I'd like to use the for because it's easier for me to see the start
and end of the block (especially when using syntax highlighting editors
like RDE).
Can anyone help be understand why the "for loop" can't count down or if
there is an alternative "for loop" syntax that I'm missing.
Thank You,
Michael