For loops don't count down

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
 
M

Michael Fellinger

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

for number in [5, 4, 3, 2, 1]
puts number
end

(oh, note that puts calls .to_s already on its own ;)

for number in (1..5).to_a.reverse
puts number
end

either is fine... however, it's not looking very nice... i really
would like to have ranges from top to bottom... not in this
ruby-version available though.

anyway, how is the syntax-highlighting different for for...end vs do...end?
and if you want matching highlighting, you can still do

5.downto(1) { |number|
puts number
}

that matches the braces, it's the recommended syntax for oneliners though.
like that:

5.downto(1){ |number| puts number }

please note also that for is just syntactic sugar to make the
transition easier for programmers of other languages, what it
essentially does is:

(1..5).to_a.reverse.each do |number|
puts number
end

hope i could help you with that a little
^ manveru
 
H

Harry

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).

Michael
I never use 'for' loops in Ruby. So I can't explain if or why not, etc.
But if you want to use 'for' loops, you could try something like this.

for number in (1..5).to_a.reverse
puts number.to_s
end


Harry
 
M

Martin DeMello

I never use 'for' loops in Ruby. So I can't explain if or why not, etc.

"for x in xs" translates internally to "xs.each do |x|", so the
underlying problem is that Range#each only counts upwards.

martin
 
D

David Vallner

what it essentially does is:

(1..5).to_a.reverse.each do |number|
puts number
end

Syntactically, I'd prefer "for number in 5..1" to that monstrous method
chain any day of the week.

David Vallner
 
R

Robert Dober

Syntactically, I'd prefer "for number in 5..1" to that monstrous method
chain any day of the week.
Completely agree, for esthetically reasoning.
But in practice, I am afraid that there is no way to have this David.
As we need 5..1 to be an empty range, or am I blocked?
David Vallner
Robert
 
S

SonOfLilit

That was already mentioned, but I wanted to show the moer general
solution since it was seeked.
 
M

Michael Brooks

Michael Fellinger wrote:
anyway, how is the syntax-highlighting different for for...end vs do...end?
and if you want matching highlighting, you can still do

5.downto(1) { |number|
puts number
}
(1..5).to_a.reverse.each do |number|
puts number
end

hope i could help you with that a little
^ manveru

Thank you for the reply. The syntax-highlighter highlights the "for"
and the "end" which are both on the left most edge of the block. I like
that because it saves me time scanning my code since I don't have to
scan to the right to realize it's a block.

I might use the "for number in (1..5).to_a.reverse" variation of the for
that Harry suggested below.

I just thought it was strange that the for couldn't do the BASIC style
step -1 and the Ruby docs didn't seem to discuss it that I could find.

Thank You!
 
M

Michael Brooks

Harry said:
I never use 'for' loops in Ruby. So I can't explain if or why not, etc.
But if you want to use 'for' loops, you could try something like this.

for number in (1..5).to_a.reverse
puts number.to_s
end



"for x in xs" translates internally to "xs.each do |x|", so the
underlying problem is that Range#each only counts upwards.

martin

Thank you Harry and Martin!

Harry, I'll probably try your "to_a.reverse" suggestion for a while
(<-no pun intended :)).

Martin, thanks for the explanation! Maybe some day the Range#each will
be enhanced to look at the ends of the range and increment or decrement
accordingly.

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top