instance method
StringScanner#pos=
(::Integer) -> ::Integer
pos=(p1)
ext/strscan/strscan.c
View on GitHub ↗
static VALUE
strscan_set_pos(VALUE self, VALUE v)
{
struct strscanner *p;
long i;
GET_SCANNER(self, p);
i = NUM2LONG(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 LONG2NUM(i);
}
call-seq: pos = n -> n pointer = n -> n
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