about for loop

P

Pat Kiatchaipipat

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??
 
J

Jari Williamsson

Pat said:
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
 

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

No members online now.

Forum statistics

Threads
474,285
Messages
2,571,416
Members
48,108
Latest member
Virus9283

Latest Threads

Top