Skip to content

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

instance method

String#upto

upto(other_string, exclusive = false) {|string| ... } → self
upto(other_string, exclusive = false) → new_enumerator

With a block given, calls the block with each String value returned by successive calls to String#succ; the first value is self, the next is self.succ, and so on; the sequence terminates when value other_string is reached; returns self:

Example
'a8'.upto('b6') {|s| print s, ' ' } # => "a8"

Output:

Example
a8 a9 b0 b1 b2 b3 b4 b5 b6

If argument exclusive is given as a truthy object, the last value is omitted:

Example
'a8'.upto('b6', true) {|s| print s, ' ' } # => "a8"

Output:

Example
a8 a9 b0 b1 b2 b3 b4 b5

If other_string would not be reached, does not call the block:

Example
'25'.upto('5') {|s| fail s }
'aa'.upto('a') {|s| fail s }

With no block given, returns a new Enumerator:

Example
'a8'.upto('b6') # => #<Enumerator: "a8":upto("b6")>
See also #upcase! #valid_encoding? #upcase also in 4.1 · 4.0 · 3.4 · 3.2 · 3.1 · 3.0