July 2011
Intermediate to advanced
400 pages
8h 46m
English
If you need to count from a specific low value up to a high value, you may use the upto() method of an integer. A block argument may optionally be used if you want to display the value at each iteration:
upto_downto.rb
0.upto(10) do
| i |
puts( i )
endThe previous code displays the integers 0 to 10. You may also count down from a high to a low value using the downto() method:
10.downto(0) do
| i |
puts( i )
endAs you can probably guess, this code displays 10 to 0.
Read now
Unlock full access