End of Life Ruby 3.1 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
IO#seek
seek(offset, whence = IO::SEEK_SET) → 0
io.c
View on GitHub ↗
static VALUE
rb_io_seek_m(int argc, VALUE *argv, VALUE io)
{
VALUE offset, ptrname;
int whence = SEEK_SET;
if (rb_scan_args(argc, argv, "11", &offset, &ptrname) == 2) {
whence = interpret_seek_whence(ptrname);
}
return rb_io_seek(io, offset, whence);
}
Seeks to the position given by integer offset (see Position) and constant whence, which is one of:
-
:CURorIO::SEEK_CUR: Repositions the stream to its current position plus the givenoffset:Examplef = File.open('t.txt') f.tell # => 0 f.seek(20, :CUR) # => 0 f.tell # => 20 f.seek(-10, :CUR) # => 0 f.tell # => 10
-
:ENDorIO::SEEK_END: Repositions the stream to its end plus the givenoffset:Examplef = File.open('t.txt') f.tell # => 0 f.seek(0, :END) # => 0 # Repositions to stream end. f.tell # => 70 f.seek(-20, :END) # => 0 f.tell # => 50 f.seek(-40, :END) # => 0 f.tell # => 30
-
:SETorIO:SEEK_SET: Repositions the stream to the givenoffset:Examplef = File.open('t.txt') f.tell # => 0 f.seek(20, :SET) # => 0 f.tell # => 20 f.seek(40, :SET) # => 0 f.tell # => 40
See also
#scroll_forward
#set_encoding
#scroll_backward
#set_encoding_by_bom
also in
4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.0