Method: StringIO#seek
- Defined in:
- stringio.c
#seek(amount, whence = SEEK_SET) ⇒ 0
Seeks to a given offset amount in the stream according to the value of whence (see IO#seek).
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 |
# File 'stringio.c', line 581
static VALUE
strio_seek(int argc, VALUE *argv, VALUE self)
{
VALUE whence;
struct StringIO *ptr = StringIO(self);
long offset;
rb_scan_args(argc, argv, "11", NULL, &whence);
offset = NUM2LONG(argv[0]);
if (CLOSED(self)) {
rb_raise(rb_eIOError, "closed stream");
}
switch (NIL_P(whence) ? 0 : NUM2LONG(whence)) {
case 0:
break;
case 1:
offset += ptr->pos;
break;
case 2:
offset += RSTRING_LEN(ptr->string);
break;
default:
error_inval("invalid whence");
}
if (offset < 0) {
error_inval(0);
}
ptr->pos = offset;
return INT2FIX(0);
}
|