times() public

Iterates block int times, passing in values from zero to int - 1.

   5.times do |i|
     print i, " "
   end

produces:

   0 1 2 3 4
Show source
Register or log in to add new notes.
October 5, 2009
3 thanks

collect/map

If you’d like to use this method for something like Enumerable#collect, you are looking at the wrong place. This method will return the initial integer, not the values from the block.

a = 20.times { |n| n * 2 } #=> 20

Instead, use Range#collect:

a = (0...20).collect { n * 2 }