Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
StringScanner#pos=
(::Integer) -> ::Integer
pos = n → n
pointer = n → n
ext/strscan/strscan.c
View on GitHub ↗
static VALUE
strscan_set_pos(VALUE self, VALUE v)
{
struct strscanner *p;
rb_len_t i;
GET_SCANNER(self, p);
i = NUM2LEN(v);
if (i < 0) i += S_LEN(p);
if (i < 0) rb_raise(rb_eRangeError, "index out of range");
if (i > S_LEN(p)) rb_raise(rb_eRangeError, "index out of range");
p->curr = i;
return LEN2NUM(i);
}
Sets the byte position and the character position; returns n.
Does not affect match values.
For non-negative n, sets the position to n:
Example
scanner = StringScanner.new(HIRAGANA_TEXT) scanner.string # => "こんにちは" scanner.pos = 3 # => 3 scanner.rest # => "んにちは" scanner.charpos # => 1
For negative n, counts from the end of the stored string:
Example
scanner.pos = -9 # => -9 scanner.pos # => 6 scanner.rest # => "にちは" scanner.charpos # => 2