# IO

The IO class provides basic IO functions.

binmode

Converts the stream into binary mode. The only way to revert to normal mode is to reopen the stream.

Returns self.

close

Closes the IO port. An Errno::EXXX exception is thrown if close fails.

Returns nil.

each\_line {|line| ... }

Reads from the IO port line by line.

Returns self.

each\_byte {|ch| ... }

Reads from the IO port byte by byte.

Returns self.

eof?

Returns true when the stream reaches the end of the file.

pos

Returns the current position of the file pointer.

pos=n

Moves the file pointer to the indicated position.

read(\[length])

Reads length bytes and returns the string. If length is omitted, reads all data until the end of the file.

If the IO has already reached the end of the file, returns nil.

Throws an Errno::EXXX exception if the data read fails and an ArgumentError exception if length is negative.

readlines(\[rs])

Reads all the data and returns an array whose elements consist of the lines read. If the IO has already reached the end of the file, returns the empty array \[].

Lines are separated by the string indicated in the argument rs, whose default value is "\n".

Specify nil for rs and the lines will not be separated. Specify the empty string "" and consecutive linefeeds will be treated as line separators (paragraph mode).

write(str)

Outputs str to the IO port. If str is not a string, tries converting it to a string with to\_s.

Returns the actual number of bytes written. Throws an Errno::EXXX exception if the output action fails.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://enls.gitbook.io/rgss-reference-manual/standard-library/built-in-classes/object/io.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
