class IO

A stream: the standard streams and open files. At the end of the stream, gets, readline and read(length) raise EOFError, gets? and read?(length) return nil, and read and read? (the rest of the stream) return "".

Sockets are streams too. An operation that has to wait for the disk, a pipe or the network suspends only the calling task. Any task may use a handle: one write call goes out whole, one read call takes a whole line or chunk, and closing a handle makes the operations waiting on it raise IOError. Using a closed stream raises IOError (closed stream), as does reading a stream opened only for writing and the reverse.

NOTE: The standard streams (STDIN, STDOUT, STDERR) are blocking for now: reading from the terminal stops the scheduler.

File.open("notes.txt", "w") { |f| f.puts "one", "two" }
f = File.open("notes.txt")
f.gets           # => "one\n"
f.read           # => "two\n"
f.gets?          # => nil
f.gets           # raises EOFError
f.close

<prelude>:6132

Methods

def puts(*args: Object) -> Void

Writes each argument followed by a newline, unless it already ends in one; an array writes each element so, nil and no argument an empty line.

<prelude>:6138

def print(*args: Object) -> Void

Writes each argument's to_s, with no separator or newline.

<prelude>:6140

def printf(format: String, *args: Object) -> Void

Writes format filled with args, as format (Kernel#format) does.

<prelude>:6143

def write(*args: Object) -> Int

Writes each argument's to_s (or a Bytes' bytes) in one write, and returns the number of bytes written. On a file open to read and write ("r+", "w+"), a write after a read goes at the read position, and a read after a write starts after what was written.

<prelude>:6148

def <<(value: Object) -> Self

Writes value's to_s and returns the stream, for chaining: io << "n = " << n << "\n".

<prelude>:6151

def gets -> String

The next line, with its newline (the last line may have none).

Raises

EOFError at the end of the stream.

<prelude>:6157

def gets? -> String?

The next line, with its newline, or nil at the end of the stream.

<prelude>:6159

def readline -> String

The next line, as #gets (EOFError at the end).

<prelude>:6161

def readlines -> Array[String]

The remaining lines, each with its newline; empty at the end.

<prelude>:6163

def read -> String

The rest of the stream; "" at the end.

<prelude>:6165

def read(length: Int) -> String

The next length bytes (fewer only at the end of the stream) as a string, "" when length is 0.

The bytes are cut where length falls, not at a character boundary, and a UTF-8 sequence cut at either end is replaced by U+FFFD: on a stream holding "hé", read(2) gives "h\u{FFFD}" (4 bytes), and the next read starts with the rest of é, replaced too. Read text by line or with #read, and binary data with #read_bytes.

Raises

EOFError when the stream is at its end.

<prelude>:6178

def read? -> String?

The rest of the stream, as #read: "" at the end, never nil.

<prelude>:6180

def read?(length: Int) -> String?

Up to length bytes, as #read(length), or nil at the end of the stream.

<prelude>:6183

def getc -> String?

The next character (a UTF-8 sequence), or nil at the end.

<prelude>:6185

def readpartial(length: Int) -> String

What is available now, up to length bytes (at least one byte, or EOFError / nil at the end).

<prelude>:6188

def readpartial?(length: Int) -> String?

What is available now, up to length bytes, waiting only when nothing is; nil at the end of the stream.

<prelude>:6191

def read_bytes(length: Int) -> Bytes

Raw bytes, up to length (empty at the end). write accepts Bytes.

<prelude>:6193

def each_line(&: (String) -> Void) -> Void

Yields each remaining line, with its newline.

<prelude>:6195

def close_write -> Void

Shuts down the writing side of a socket: the peer reads the end of the stream, and the socket can still be read.

Raises

TypeError on a stream that is not a connected socket.

<prelude>:6202

def close_read -> Void

Shuts down the reading side of a socket.

Raises

TypeError on a stream that is not a connected socket.

<prelude>:6208

def flush -> Void

Writes out what the stream has buffered.

<prelude>:6210

def close -> Void

Flushes and closes the stream; operations waiting on it raise IOError.

Closing again does nothing, and so does closing a standard stream.

<prelude>:6214

def closed? -> Bool

Whether the stream has been closed.

<prelude>:6216

def eof? -> Bool

Whether a read would find the end of the stream, waiting for input if none is buffered.

True for a stream not opened for reading.

<prelude>:6221

def rewind -> Void

Goes back to the start of a file, for reading and writing.

Raises

IOError on a socket.

<prelude>:6227

def tty? -> Bool

Whether the stream is a terminal (only a standard stream can be).

<prelude>:6229

def path -> String?

The path a file was opened with or a UNIX socket is bound to; nil for the other streams.

<prelude>:6232

def to_s -> String

As #inspect: #<File:notes.txt>, #<IO:<STDOUT>>.

<prelude>:6234

def inspect -> String

A description of the stream: #<File:notes.txt>, #<IO:<STDOUT>>.

<prelude>:6236