The upto Method and for loop : upto « Statement « Ruby






The upto Method and for loop


# The upto method is a convenience method that does the same thing as a for loop
# The Integer, String, and Date classes all have upto methods

for i in 1..10
  print i, " "
end
# => 1 2 3 4 5 6 7 8 9 10


# Compare this with upto, which does exactly the same thing:

1.upto(10) { |i| print i, " " } # => 1 2 3 4 5 6 7 8 9 10

 








Related examples in the same category

1.use the upto iterator, which is what traditional for loops translate into in Ruby.
2.Use an upto Iterator to reference array element
3.uses the upto iterator to create a loop similar to a for loop from other languages
4.use curly braces ({}):
5.Example of upto that prints out a times table for 2
6.do all the times tables from 1 to 12
7.One date may upto another
8.Interpolation with upto and array
9.upto with / without {}