このマニュアルは既にメンテナンスが終了したバージョンの Ruby を対象としています。 最新版のマニュアルへ

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

singleton method IO.binread

binread(path, length = nil, offset = 0) -> String | nilRuby 1.9.3 から[permalink][rdoc][edit]

path で指定したファイルを open し、offset の所まで seek し、 length バイト読み込みます。

Kernel.#open と同様 path の先頭が "|" ならば、"|" に続くコマンドの出力を読み取ります。 IO.read と同様、この "|コマンド名" の特別扱いはレシーバが IO である場合のみ有効で、File.binread では単なるファイル名として扱われます。

length を省略するとファイルの末尾まで読み込みます。

ファイルを開くときの mode は "rb:ASCII-8BIT" です。

IO.write("testfile", "This is line one\nThis is line two\nThis is line three\nAnd so on...\n")
p IO.binread("testfile")         # => "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
p IO.binread("testfile", 20)     # => "This is line one\nThi"
p IO.binread("testfile", 20, 10) # => "ne one\nThis is line "

[SEE_ALSO] IO.read