Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
StringScanner#matched_size
() -> ::Integer?
matched_size → substring_size or nil
ext/strscan/strscan.c
View on GitHub ↗
static VALUE
strscan_matched_size(VALUE self)
{
struct strscanner *p;
rb_len_t beg, end;
GET_SCANNER(self, p);
if (! MATCHED_P(p)) return Qnil;
beg = adjust_register_position(p, p->regs.beg[0]);
if (beg > S_LEN(p)) return Qnil;
end = minl(adjust_register_position(p, p->regs.end[0]), S_LEN(p));
return LEN2NUM(end - beg);
}
Returns the size (in bytes) of the matched substring from the most recent match match attempt if it was successful, or nil otherwise; see Basic Matched Values:
Example
scanner = StringScanner.new('foobarbaz') scanner.matched_size # => nil pos = 3 scanner.exist?(/baz/) # => 9 scanner.matched_size # => 3 scanner.exist?(/nope/) # => nil scanner.matched_size # => nil