How to write negative loop in ruby like for(i=index; i >= 0; i --)

前端 未结 7 1416
滥情空心
滥情空心 2020-12-25 10:42

How can I write a loop in Ruby that counts down, similar to the following C-style for loop?

for (i = 25; i >= 0; i--) { 
    print i;
}
         


        
7条回答
  •  醉酒成梦
    2020-12-25 11:19

    downto is fine, but there is also the more generic step.

    25.step(0, -1){|i| puts i}
    

提交回复
热议问题