Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
instance method
IO::Buffer#pread
(untyped, untyped, untyped) -> untyped
pread(io, from, [offset, [length]]) → read length or -errno
io_buffer.c
View on GitHub ↗
static VALUE
io_buffer_pread(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 2, 4);
VALUE io = argv[0];
rb_off_t from = NUM2OFFT(argv[1]);
size_t offset, length;
io_buffer_extract_offset_length(self, argc-2, argv+2, &offset, &length);
return rb_io_buffer_pread(self, io, from, offset, length);
}
Perform one read operation of at most length bytes from io at from into the buffer starting at offset. A short read is a normal result and the IO’s current position is not modified. If an error occurs, return -errno.
If offset is not given, it defaults to zero, i.e. the beginning of the buffer. If length is not given, it defaults to the size of the buffer minus the offset. A zero length is a no-op.
Example
IO::Buffer.for('test') do |buffer| p buffer # => # <IO::Buffer 0x00007fca40087c38+4 SLICE> # 0x00000000 74 65 73 74 test # take 2 bytes from the beginning of urandom, # put them in buffer starting from position 2 buffer.pread(File.open('/dev/urandom', 'rb'), 0, 2, 2) p buffer # => # <IO::Buffer 0x00007f3bc65f2a58+4 EXTERNAL SLICE> # 0x00000000 05 35 73 74 te.5 end