Pseudo I/O on String object.

Commonly used to simulate `$stdio` or `$stderr`

Examples

require 'stringio'

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

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


No documentation available

This is a deprecated alias for each_byte.

This is a deprecated alias for each_char.

Closes strio. The strio 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 strio is not readable.

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

Returns true if strio is completely closed, false otherwise.

Returns true if strio is not readable, false otherwise.

Returns true if strio is not writable, false otherwise.

This is a deprecated alias for each_codepoint.

See IO#each.

An alias for each

Returns true if strio is at end of file. The stringio 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 strio is write mode and no encoding is specified, returns nil.

Returns nil. Just for compatibility to IO.

Returns strio 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 in strio. The stringio 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) of strio.

Seeks to the given position (in bytes) in strio.

See IO#putc.

See IO#read.

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

Positions strio 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.

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) of strio.

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

An alias for isatty

Pushes back one character (passed as a parameter) onto strio 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 of strio. 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.