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

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

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

If offset is not given, put it at the beginning of the buffer.

Example:

Example
IO::Buffer.for('test') do |buffer|
  p buffer
  # =>
  # <IO::Buffer 0x00007fca40087c38+4 SLICE>
  # 0x00000000  74 65 73 74         test

  # take 2 bytes from the beginning of urandom,
  # put them in buffer starting from position 2
  buffer.pread(File.open('/dev/urandom', 'rb'), 0, 2, 2)
  p buffer
  # =>
  # <IO::Buffer 0x00007f3bc65f2a58+4 EXTERNAL SLICE>
  # 0x00000000  05 35 73 74         te.5
end
See also #or! #pwrite #null? #read also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1