Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
Integer#times
times {|i| ... } → self
times → enumerator
numeric.rb
View on GitHub ↗
# File tmp/rubies/ruby-3.3.0/numeric.rb, line 231
def times
unless block_given?
return to_enum(:times) { self < 0 ? 0 : self }
end
i = 0
while i < self
yield i
i = i.succ
end
self
end
Calls the given block self times with each integer in (0..self-1):
Example
a = [] 5.times {|i| a.push(i) } # => 5 a # => [0, 1, 2, 3, 4]
With no block given, returns an Enumerator.