read(p1, p2 = v2, p3 = v3, p4 = v4, p5 = {}) public

Opens the file, optionally seeks to the given offset, then returns length bytes (defaulting to the rest of the file). read ensures the file is closed before returning.

Options

The options hash accepts the following keys:

encoding

string or encoding

Specifies the encoding of the read string. encoding: will be ignored if length is specified. See Encoding.aliases for possible encodings.

mode

string

Specifies the mode argument for open(). It must start with an “r” otherwise it will cause an error. See IO.new for the list of possible modes.

open_args

array of strings

Specifies arguments for open() as an array. This key can not be used in combination with either encoding: or mode:.

Examples:

IO.read("testfile")              #=> "This is line one\nThis is line two\nThis is line three\nAnd so on...\n"
IO.read("testfile", 20)          #=> "This is line one\nThi"
IO.read("testfile", 20, 10)      #=> "ne one\nThis is line "
IO.read("binfile", mode: "rb")   #=> "\xF7\x00\x00\x0E\x12"
Show source
Register or log in to add new notes.