method

times

v1_9_1_378 - Show latest stable - Class: Integer
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

1Note

collect/map

Mange ยท Oct 5, 20093 thanks

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 }