Ruby 4.0 リファレンスマニュアル

instance method StringIO#pread

pread(maxlen, offset) -> StringRuby 3.3 から[permalink][rdoc][edit]
pread(maxlen, offset, outbuf) -> String

自身の現在の読み込み位置を変更せずに、offset から maxlen バイト読み込んで返します。

IO#pread と違いシステムコールを使わないので、常にアトミックです。

[PARAM] maxlen:
読み込むバイト数を指定します。
[PARAM] offset:
読み込み開始位置を先頭からのオフセットで指定します。
[PARAM] outbuf:
読み込んだデータを格納する String を指定します。
[EXCEPTION] EOFError:
offset が末尾以降を指している場合に発生します。
[EXCEPTION] IOError:
自身が読み込み用にオープンされていない場合に発生します。
require 'stringio'

s = StringIO.new("hello world")
p s.pread(5, 6)          # => "world"
buf = String.new
p s.pread(5, 0, buf)      # => "hello"
p buf                     # => "hello"

[SEE_ALSO] IO#pread