Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

instance method

IO::Buffer#read

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

Perform one read operation of at most length bytes from io into the buffer starting at offset. A short read is a normal result. 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.

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'), 0, 2)
  p buffer
  # =>
  # <IO::Buffer 0x00007f3bc65f2a58+4 EXTERNAL SLICE>
  # 0x00000000  05 35 73 74         .5st
end
See also #pwrite #readonly? #pread #set_string also in 4.0 · 3.4 · 3.3 · 3.2 · 3.1