End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
StringScanner#peek
peek(len)
ext/strscan/strscan.c
View on GitHub ↗
static VALUE
strscan_peek(VALUE self, VALUE vlen)
{
struct strscanner *p;
long len;
GET_SCANNER(self, p);
len = NUM2LONG(vlen);
if (EOS_P(p))
return str_new(p, "", 0);
len = minl(len, S_RESTLEN(p));
return extract_beg_len(p, p->curr, len);
}
Extracts a string corresponding to string[pos,len], without advancing the scan pointer.
Example
s = StringScanner.new('test string') s.peek(7) # => "test st" s.peek(7) # => "test st"