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"

Puts stream into binary mode. See IO#binmode.

bytes

#

This is a deprecated alias for each_byte.

chars

#

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.

codepoints

#

This is a deprecated alias for each_codepoint.

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.

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.

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

fcntl

#

fileno

#

Returns nil. Just for compatibility to IO.

flush

#

Returns an object itself. Just for compatibility to IO.

fsync

#

Returns 0. Just for compatibility to IO.

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

isatty

#

Returns false. Just for compatibility to IO.

Returns the size of the buffer string.

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.

lines

#

This is a deprecated alias for each_line.

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.

pid

#

Returns nil. Just for compatibility to IO.

Returns the current offset (in bytes).

Seeks to the given position (in bytes).

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.

set_encoding_by_bom

#
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.

sync=

#

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.

tty?

#

Returns false. Just for compatibility to IO.

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.