Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
instance method
IO::Buffer#read
read(io, [length, [offset]]) → read length or -errno
io_buffer.c
View on GitHub ↗
static VALUE
io_buffer_read(int argc, VALUE *argv, VALUE self)
{
rb_check_arity(argc, 1, 3);
VALUE io = argv[0];
size_t length, offset;
io_buffer_extract_length_offset(self, argc-1, argv+1, &length, &offset);
return rb_io_buffer_read(self, io, length, offset);
}
Read at least length bytes from the io, into the buffer starting at offset. If an error occurs, return -errno.
If length is not given or nil, it defaults to the size of the buffer minus the offset, i.e. the entire buffer.
If length is zero, exactly one read operation will occur.
If offset is not given, it defaults to zero, i.e. the beginning of the buffer.
Example
IO::Buffer.for('test') do |buffer| p buffer # => # <IO::Buffer 0x00007fca40087c38+4 SLICE> # 0x00000000 74 65 73 74 test buffer.read(File.open('/dev/urandom', 'rb'), 2) p buffer # => # <IO::Buffer 0x00007f3bc65f2a58+4 EXTERNAL SLICE> # 0x00000000 05 35 73 74 .5st end