Method: StringIO#seek

Defined in:
ext/stringio/stringio.c

#seek(offset, whence = SEEK_SET) ⇒ 0

Sets the current position to the given integer offset (in bytes), with respect to a given constant whence; see Position.

Returns:

  • (0)


785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
# File 'ext/stringio/stringio.c', line 785

static VALUE
strio_seek(int argc, VALUE *argv, VALUE self)
{
    VALUE whence;
    struct StringIO *ptr = StringIO(self);
    long amount, offset;

    rb_scan_args(argc, argv, "11", NULL, &whence);
    amount = NUM2LONG(argv[0]);
    if (CLOSED(self)) {
  rb_raise(rb_eIOError, "closed stream");
    }
    switch (NIL_P(whence) ? 0 : NUM2LONG(whence)) {
      case 0:
  offset = 0;
  break;
      case 1:
  offset = ptr->pos;
  break;
      case 2:
  offset = RSTRING_LEN(ptr->string);
  break;
      default:
  error_inval("invalid whence");
    }
    if (amount > LONG_MAX - offset || amount + offset < 0) {
  error_inval(0);
    }
    ptr->pos = amount + offset;
    return INT2FIX(0);
}