Skip to content

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#read

read(io, [length, [offset]]) → read length or -errno

Read at most length bytes from io into the buffer, starting at offset. If an error occurs, return -errno.

If length is not given, read until the end of the buffer.

If offset is not given, read from the beginning of the buffer.

If length is 0, read nothing.

Example:

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
See also #pwrite #readonly? #pread #resize also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1