Forum: Ruby Newbie questoin of the day:A for loop that counts backwards?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
3df897745c664e3add6f047daa5835d1?d=identicon&s=25 MenDAKE (Guest)
on 2006-04-21 21:23
How do I get a for loop to count backwards?

I have the loop...

for i in 1 ... 8

...but I want it to start at 8 and end at 1. This does NOT work:

for i in 8 ... 1

Thanks for helping out a newbie.
31e038e4e9330f6c75ccfd1fca8010ee?d=identicon&s=25 Gregory Brown (Guest)
on 2006-04-21 21:29
(Received via mailing list)
On 4/21/06, MenDAKE <mendake_ddude@yahoo.com> wrote:
> How do I get a for loop to count backwards?
>
> I have the loop...
>
> for i in 1 ... 8

1.upto(8) { |i| .. }

> ...but I want it to start at 8 and end at 1. This does NOT work:
>
> for i in 8 ... 1

8.downto(1) { |i| ... }
B1bfd91125fe17724594fd422eae8bec?d=identicon&s=25 Guest (Guest)
on 2006-04-21 22:41
MenDAKE wrote:
> How do I get a for loop to count backwards?
>
> I have the loop...
>
> for i in 1 ... 8
>
> ...but I want it to start at 8 and end at 1. This does NOT work:
>
> for i in 8 ... 1
>
> Thanks for helping out a newbie.

I like doing it this way... it makes more sense to me:

x = 8
while x > 0
  x = x-1
 puts x
end
81d609425e306219d54d793a0ad98bce?d=identicon&s=25 Matthew Moss (Guest)
on 2006-04-21 22:52
(Received via mailing list)
(1...8).reverse.each { |x| puts x }
81d609425e306219d54d793a0ad98bce?d=identicon&s=25 Matthew Moss (Guest)
on 2006-04-21 22:52
(Received via mailing list)
Ooops, one too many periods.  That should be:  (1..8)
0b561a629b87f0bbf71b45ee5a48febb?d=identicon&s=25 Dave Burt (Guest)
on 2006-04-21 23:48
(Received via mailing list)
Matthew Moss wrote:
> (1..8).reverse.each { |x| puts x }

And if you like your "for" syntax:

for i in (1..8).reverse
  ...

Cheers,
Dave
81d609425e306219d54d793a0ad98bce?d=identicon&s=25 Matthew Moss (Guest)
on 2006-04-22 00:04
(Received via mailing list)
Not me...  "for" can go hang out in my C++ code with the other schmuck
keywords.
05be5d6610e2c3f1780aa0e39e902e93?d=identicon&s=25 Farrel Lifson (Guest)
on 2006-04-22 00:10
(Received via mailing list)
8.downto(1) {|x| puts x}
3df897745c664e3add6f047daa5835d1?d=identicon&s=25 MenDAKE (Guest)
on 2006-04-22 18:36
Wow, so many options. What a great language. Thanks, everyone.
Dedb38b3571b323b77bc9b221e940172?d=identicon&s=25 ruby talk (Guest)
on 2006-04-22 19:01
(Received via mailing list)
max=9
for i in 1..8
p max-i
end


its not very ruby like. but it works
49ab3ce5a4922b4747d1d6f330784629?d=identicon&s=25 Jake McArthur (Guest)
on 2006-04-23 00:11
(Received via mailing list)
<sarcasm>

def countdown(from, to)
   yield from
   countdown(from-1, to) { |x| yield x } if from > to
end

countdown(8, 1) { |x| puts x }

</sarcasm>

Just playing with you a little.

- Jake McArthur
This topic is locked and can not be replied to.