Ruby IO Functions

EditRocket provides the following information on IO functions in the Ruby source code builder.

binmode - Puts ios into binary mode. This is useful only in MS-DOS/Windows environments. Once a stream is in binary mode, it cannot be reset to nonbinary mode.

close - Closes ios and flushes any pending writes to the operating system. The stream is unavailable for any further data operations; an IOError is raised if such an attempt is made. I/O streams are automatically closed when they are claimed by the garbage collector.

close_read - Closes the read end of a duplex I/O stream (i.e., one that contains both a read and a write stream, such as a pipe). Will raise an IOError if the stream is not duplexed.

close_write - Closes the write end of a duplex I/O stream (i.e., one that contains both a read and a write stream, such as a pipe). Will raise an IOError if the stream is not duplexed.

closed? - Returns true if ios is completely closed (for duplex streams, both reader and writer), false otherwise.

each(sep_string=$/) - Executes the block for every line in ios, where lines are separated by sep_string. ios must be opened for reading or an IOError will be raised.

each_byte - Calls the given block once for each byte (0..255) in ios, passing the byte as an argument. The stream must be opened for reading or an IOError will be raised.

each_line(sep_string=$/) - Executes the block for every line in ios, where lines are separated by sep_string. ios must be opened for reading or an IOError will be raised.

eof? - Returns true if ios is at end of file that means there are no more data to read. The stream must be opened for reading or an IOError will be raised.

eof - Returns true if ios is at end of file that means there are no more data to read. The stream must be opened for reading or an IOError will be raised.

fcntl(integer_cmd, arg) - Provides a mechanism for issuing low-level commands to control or query file-oriented I/O streams. Arguments and results are platform dependent. If arg is a number, its value is passed directly. If it is a string, it is interpreted as a binary sequence of bytes (Array#pack might be a useful way to build this string). On Unix platforms, see fcntl(2) for details. Not implemented on all platforms.

fileno - Returns an integer representing the numeric file descriptor for ios.

flush - Flushes any buffered data within ios to the underlying operating system (note that this is Ruby internal buffering only; the OS may buffer the data as well).

for_fd(fd, mode) - Synonym for IO::new.

foreach(name, sep_string=$/) - Executes the block for every line in the named I/O port, where lines are separated by sep_string.

fsync - Immediately writes all buffered data in ios to disk. Returns nil if the underlying operating system does not support fsync(2). Note that fsync differs from using IO#sync=. The latter ensures that data is flushed from Ruby's buffers, but doesn't not guarantee that the underlying operating system actually writes it to disk.

getc - Gets the next 8-bit byte (0..255) from ios. Returns nil if called at end of file.

gets(sep_string=$/) - Reads the next ``line'' from the I/O stream; lines are separated by sep_string. A separator of nil reads the entire contents, and a zero-length separator reads the input a paragraph at a time (two successive newlines in the input separate paragraphs). The stream must be opened for reading or an IOError will be raised. The line read in will be returned and also assigned to $_. Returns nil if called at end of file.

inspect - Return a string describing this IO object.

ioctl(integer_cmd, arg) - Provides a mechanism for issuing low-level commands to control or query I/O devices. Arguments and results are platform dependent. If arg is a number, its value is passed directly. If it is a string, it is interpreted as a binary sequence of bytes. On Unix platforms, see ioctl(2) for details. Not implemented on all platforms.

isatty - Returns true if ios is associated with a terminal device (tty), false otherwise.

lineno= - Manually sets the current line number to the given value. $. is updated only on the next read.

lineno - Returns the current line number in ios. The stream must be opened for reading. lineno counts the number of times gets is called, rather than the number of newlines encountered. The two values will differ if gets is called with a separator other than newline. See also the $. variable.

new(fd, mode_string) - Returns a new IO object (a stream) for the given integer file descriptor and mode string. See also IO#fileno and IO::for_fd.

open(fd, mode_string="r" ) - With no associated block, open is a synonym for IO::new. If the optional code block is given, it will be passed io as an argument, and the IO object will automatically be closed when the block terminates. In this instance, IO::open returns the value of the block.

pid - Returns the process ID of a child process associated with ios. This will be set by IO::popen.

pipe - "Creates a pair of pipe endpoints (connected to each other) and returns them as a two-element array of IO objects: [ read_file, write_file ]. Not available on all platforms."

popen(cmd_string, mode="r" ) - Runs the specified command string as a subprocess; the subprocess's standard input and output will be connected to the returned IO object. If cmd_string starts with a ``-'', then a new instance of Ruby is started as the subprocess. The default mode for the new file object is ``r'', but mode may be set to any of the modes listed in the description for class IO.

pos= - Seeks to the given position (in bytes) in ios.

pos - Returns the current offset (in bytes) of ios.

print() - Writes the given object(s) to ios. The stream must be opened for writing. If the output record separator ($\) is not nil, it will be appended to the output. If no arguments are given, prints $_. Objects that aren't strings will be converted by calling their to_s method. With no argument, prints the contents of the variable $_. Returns nil.

printf(format_string [, obj, ...] ) - Formats and writes to ios, converting parameters under control of the format string. See Kernel#sprintf for details.

putc(obj) - If obj is Numeric, write the character whose code is obj, otherwise write the first character of the string representation of obj to ios.

puts(obj, ...) - Writes the given objects to ios as with IO#print. Writes a record separator (typically a newline) after any that do not already end with a newline sequence. If called with an array argument, writes each element on a new line. If called without arguments, outputs a single record separator.

read(name, [length [, offset]] ) - 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.

read(name, [length [, offset]] ) - 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.

read_nonblock(maxlen) - Reads at most maxlen bytes from ios using read(2) system call after O_NONBLOCK is set for the underlying file descriptor.

readbytes(n) - Reads exactly n bytes.

readchar - Reads a character as with IO#getc, but raises an EOFError on end of file.

readline(sep_string=$/) - Reads a line as with IO#gets, but raises an EOFError on end of file.

readlines(name, sep_string=$/) - Reads the entire file specified by name as individual lines, and returns those lines in an array. Lines are separated by sep_string.

readlines(name, sep_string=$/) - Reads the entire file specified by name as individual lines, and returns those lines in an array. Lines are separated by sep_string.

readpartial(maxlen) - Reads at most maxlen bytes from the I/O stream. It blocks only if ios has no data immediately available. It doesn't block if some data available. If the optional outbuf argument is present, it must reference a String, which will receive the data. It raises EOFError on end of file.

reopen(other_IO) - Reassociates ios with the I/O stream given in other_IO or to a new stream opened on path. This may dynamically change the actual class of this stream.

rewind - Positions ios to the beginning of input, resetting lineno to zero.

scanf(str,&b) - The trick here is doing a match where you grab one line of input at a time. The linebreak may or may not occur at the boundary where the string matches a format specifier. And if it does, some rule about whitespace may or may not be in effect...

seek(amount, whence=SEEK_SET) - "Seeks to a given offset anInteger in the stream according to the value of whence:"

select(read_array - See Kernel#select.

stat - Returns status information for ios as an object of type File::Stat.

sync= - Sets the ``sync mode'' to true or false. When sync mode is true, all output is immediately flushed to the underlying operating system and is not buffered internally. Returns the new state. See also IO#fsync.

sync - Returns the current ``sync mode'' of ios. When sync mode is true, all output is immediately flushed to the underlying operating system and is not buffered by Ruby internally. See also IO#fsync.

sysopen(path, [mode, [perm]]) - Opens the given path, returning the underlying file descriptor as a Fixnum.

sysread(integer ) - Reads integer bytes from ios using a low-level read and returns them as a string. Do not mix with other methods that read from ios or you may get unpredictable results. Raises SystemCallError on error and EOFError at end of file.

sysseek(offset, whence=SEEK_SET) - Seeks to a given offset in the stream according to the value of whence (see IO#seek for values of whence). Returns the new offset into the file.

syswrite(string) - Writes the given string to ios using a low-level write. Returns the number of bytes written. Do not mix with other methods that write to ios or you may get unpredictable results. Raises SystemCallError on error.

tell - Returns the current offset (in bytes) of ios.

to_i() - "Alias for #fileno"

to_io - Returns ios.

tty? - Returns true if ios is associated with a terminal device (tty), false otherwise.

ungetc(integer) - Pushes back one character (passed as a parameter) onto ios, such that a subsequent buffered read will return it. Only one character may be pushed back before a subsequent read operation (that is, you will be able to read only the last of several characters that have been pushed back). Has no effect with unbuffered reads (such as IO#sysread).

write(string) - Writes the given string to ios. The stream must be opened for writing. If the argument is not a string, it will be converted to a string using to_s. Returns the number of bytes written.

write_nonblock(string) - Writes the given string to ios using write(2) system call after O_NONBLOCK is set for the underlying file descriptor.