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#pwrite
pwrite(io, from, length, [offset]) → written length or -errno
io_buffer.c
View on GitHub ↗
static VALUE
io_buffer_pwrite(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_pwrite(self, io, from, length, offset);
}
Writes length bytes from buffer into io, starting at offset in the buffer. If an error occurs, return -errno.
If offset is not given, the bytes are taken from the beginning of the buffer. If the offset is given and is beyond the end of the file, the gap will be filled with null (0 value) bytes.
Example
out = File.open('output.txt', File::RDWR) # open for read/write, no truncation IO::Buffer.for('1234567').pwrite(out, 2, 3, 1)
This leads to 234 (3 bytes, starting from position 1) being written into output.txt, starting from file position 2.