Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

StringScanner#check

check(pattern)

This returns the value that scan would return, without advancing the scan pointer. The match register is affected, though.

Example
s = StringScanner.new("Fri Dec 12 1975 14:39")
s.check /Fri/               # -> "Fri"
s.pos                       # -> 0
s.matched                   # -> "Fri"
s.check /12/                # -> nil
s.matched                   # -> nil

Mnemonic: it “checks” to see whether a scan will return a value.

See also #charpos #check_until #captures #clear also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0