Method: IO#seek
- Defined in:
- io.c
#seek(offset, whence = IO::SEEK_SET) ⇒ 0
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:f = File.open('t.txt') f.tell # => 0 f.seek(20, :CUR) # => 0 f.tell # => 20 f.seek(-10, :CUR) # => 0 f.tell # => 10 f.close -
:ENDorIO::SEEK_END: Repositions the stream to its end plus the givenoffset:f = File.open('t.txt') f.tell # => 0 f.seek(0, :END) # => 0 # Repositions to stream end. f.tell # => 52 f.seek(-20, :END) # => 0 f.tell # => 32 f.seek(-40, :END) # => 0 f.tell # => 12 f.close -
:SETorIO:SEEK_SET: Repositions the stream to the givenoffset:f = File.open('t.txt') f.tell # => 0 f.seek(20, :SET) # => 0 f.tell # => 20 f.seek(40, :SET) # => 0 f.tell # => 40 f.close
Related: IO#pos=, IO#tell.
2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 |
# File 'io.c', line 2521
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);
}
|