Class

Pseudo I/O on String object, with interface corresponding to IO.

Commonly used to simulate $stdio or $stderr

Examples

require 'stringio'

# Writing stream emulation
io = StringIO.new
io.puts "Hello World"
io.string #=> "Hello World\n"

# Reading stream emulation
io = StringIO.new "first\nsecond\nlast\n"
io.getc #=> "f"
io.gets #=> "irst\n"
io.read #=> "second\nlast\n"
Constants
No documentation available
Class Methods

Creates new StringIO instance from with string and mode.

Equivalent to StringIO.new except that when it is called with a block, it yields with the new instance and closes it, and returns the result which returned from the block.

Instance Methods

Puts stream into binary mode. See IO#binmode.

This is a deprecated alias for each_byte.

This is a deprecated alias for each_char.

Closes a StringIO. The stream is unavailable for any further data operations; an IOError is raised if such an attempt is made.

Closes the read end of a StringIO. Will raise an IOError if the receiver is not readable.

Closes the write end of a StringIO. Will raise an IOError if the receiver is not writeable.

Returns true if the stream is completely closed, false otherwise.

Returns true if the stream is not readable, false otherwise.

Returns true if the stream is not writable, false otherwise.

This is a deprecated alias for each_codepoint.

See IO#each.

Returns true if the stream is at the end of the data (underlying string). The stream must be opened for reading or an IOError will be raised.

An alias for eof

Returns the Encoding object that represents the encoding of the file. If the stream is write mode and no encoding is specified, returns nil.

Returns nil. Just for compatibility to IO.

Returns an object itself. Just for compatibility to IO.

Returns 0. Just for compatibility to IO.

See IO#getbyte.

See IO#getc.

See IO#gets.

Returns the Encoding of the internal string if conversion is specified. Otherwise returns nil.

Returns false. Just for compatibility to IO.

An alias for size

Returns the current line number. 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.

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

This is a deprecated alias for each_line.

Returns nil. Just for compatibility to IO.

Returns the current offset (in bytes).

Seeks to the given position (in bytes).

See IO#putc.

See IO#read.

Reinitializes the stream with the given other_StrIO or string and mode (see StringIO#new).

Positions the stream to the beginning of input, resetting lineno to zero.

Seeks to a given offset amount in the stream according to the value of whence (see IO#seek).

Specify the encoding of the StringIO as ext_enc. Use the default external encoding if ext_enc is nil. 2nd argument int_enc and optional hash opt argument are ignored; they are for API compatibility to IO.

No documentation available

Returns the size of the buffer string.

Returns underlying String object, the subject of IO.

Changes underlying String object, the subject of IO.

Returns true always.

Returns the argument unchanged. Just for compatibility to IO.

Returns the current offset (in bytes).

Truncates the buffer string to at most integer bytes. The stream must be opened for writing.

An alias for isatty

Pushes back one character (passed as a parameter) such that a subsequent buffered read will return it. There is no limitation for multiple pushbacks including pushing back behind the beginning of the buffer string.

Appends the given string to the underlying buffer string. 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. See IO#write.