Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →
IO::Buffer#pwrite
static VALUE
io_buffer_pwrite(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_pwrite(self, io, from, offset, length);
}
Perform one write operation of at most length bytes to io at from from the buffer starting at offset. A short write 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.
If the from position is beyond the end of the file, the gap will be filled with null (0 value) bytes.
out = File.open('output.txt', File::RDWR) # open for read/write, no truncation IO::Buffer.for('1234567').pwrite(out, 2, 1, 3)
This leads to 234 (3 bytes, starting from position 1) being written into output.txt, starting from file position 2.