Skip to content

End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

class method

IO.read

IO.read(command, length = nil, offset = 0, **opts) → string or nil
IO.read(path, length = nil, offset = 0, **opts) → string or nil

Opens the stream, reads and returns some or all of its content, and closes the stream; returns nil if no bytes were read.

When called from class IO (but not subclasses of IO), this method has potential security vulnerabilities if called with untrusted input; see Command Injection.

The first argument must be a string; its meaning depends on whether it starts with the pipe character ('|'):

  • If so (and if self is IO), the rest of the string is a command to be executed as a subprocess.

  • Otherwise, the string is the path to a file.

With only argument command given, executes the command in a shell, returns its entire $stdout:

Example
IO.read('| cat t.txt')
# => "First line\nSecond line\n\nThird line\nFourth line\n"

With only argument path given, reads in text mode and returns the entire content of the file at the given path:

Example
IO.read('t.txt')
# => "First line\nSecond line\n\nThird line\nFourth line\n"

On Windows, text mode can terminate reading and leave bytes in the file unread when encountering certain special bytes. Consider using IO.binread if all bytes in the file should be read.

For both forms, command and path, the remaining arguments are the same.

With argument length, returns length bytes if available:

Example
IO.read('t.txt', 7) # => "First l"
IO.read('t.txt', 700)
# => "First line\r\nSecond line\r\n\r\nFourth line\r\nFifth line\r\n"

With arguments length and offset, returns length bytes if available, beginning at the given offset:

Example
IO.read('t.txt', 10, 2)   # => "rst line\nS"
IO.read('t.txt', 10, 200) # => nil

Optional keyword arguments opts specify:

See also #read .popen .readlines .pipe also in 4.1 · 4.0 · 3.4 · 3.3 · 3.1 · 3.0