End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
instance method
IO::Buffer#pread
pread(io, from, length, [offset]) → 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, 3, 4);
VALUE io = argv[0];
rb_off_t from = NUM2OFFT(argv[1]);
size_t length;
if (rb_int_negative_p(argv[2])) {
rb_raise(rb_eArgError, "Length can't be negative!");
}
length = NUM2SIZET(argv[2]);
size_t offset = 0;
if (argc >= 4) {
if (rb_int_negative_p(argv[3])) {
rb_raise(rb_eArgError, "Offset can't be negative!");
}
offset = NUM2SIZET(argv[3]);
}
return rb_io_buffer_pread(self, io, from, length, offset);
}
Read at most length bytes from io into the buffer, starting at from, and put it in buffer starting from specified offset. If an error occurs, return -errno.
If offset is not given, put it at the beginning of the buffer.
Example:
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