Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

StringScanner#peek

(::Integer) -> ::String
peek(length) → substring

Returns the substring string[pos, length]; does not update match values or positions:

Example
scanner = StringScanner.new('foobarbaz')
scanner.pos = 3
scanner.peek(3)   # => "bar"
scanner.terminate
scanner.peek(3)   # => ""
See also #named_captures #peek_byte #matched_size #pointer also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0