Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

instance method

Integer#upto

(::Numeric limit) { (::Integer) -> void } -> ::Integer
(::Numeric limit) -> ::Enumerator[::Integer, self]
upto(limit) {|i| ... } → self
upto(limit) → enumerator

Calls the given block with each integer value from self up to limit; returns self:

Example
a = []
5.upto(10) {|i| a << i }              # => 5
a                                     # => [5, 6, 7, 8, 9, 10]
a = []
-5.upto(0) {|i| a << i }              # => -5
a                                     # => [-5, -4, -3, -2, -1, 0]
5.upto(4) {|i| fail 'Cannot happen' } # => 5

With no block given, returns an Enumerator.

See also #truncate #zero? #to_s also in 4.1 · 4.0 · 3.3 · 3.2 · 3.1 · 3.0