How can I write a loop in Ruby that counts down, similar to the following C-style for loop?
for
for (i = 25; i >= 0; i--) { print i; }
There are many ways to perform a decrementing loop in Ruby:
First way:
for i in (10).downto(0) puts i end
Second way:
(10).downto(0) do |i| puts i end
Third way:
i=10; until i<0 puts i i-=1 end
2.1m questions
2.1m answers
60 comments
57.0k users