Monday, March 9, 2009

Looping Through Numbers with Blocks and Iterators

5.times do puts "Test" end
1.upto(5) { ...code to loop here... }
10.downto(5) { ...code to loop here... }
0.step(50, 5) { ...code to loop here... }
The first example counts from 1 “up to” 5. The second example counts from 10 “down
to” 5. The last example counts up from 0 to 50 in steps of 5, because you’re using the step
method on the number 0.