[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

about for loop

Pat Kiatchaipipat

3/20/2008 7:30:00 AM

for i in 0..5
print i
end

it show me '012345'.
and then I want to show '543210' and I try.

for i in 5..0
print i
end

it doesn't work :'(.
how can I do it??
--
Posted via http://www.ruby-....

1 Answer

Jari Williamsson

3/20/2008 7:53:00 AM

0

Pat Kiatchaipipat wrote:
> for i in 0..5
> print i
> end

For loops are not very rubyish. Please use an internal iterator instead.
For example:
(0..5).each { |i| print i }
or
0.upto(5) { |i| print i }
or
6.times { |i| print i }


> it show me '012345'.
> and then I want to show '543210' and I try.
>
> for i in 5..0
> print i
> end
>
> it doesn't work :'(.
> how can I do it??

5.downto(0) { |i| print i }


Best regards,

Jari Williamsson