123456789_123456789_123456789_123456789_123456789_

Class: StringIO

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, ::Data
Instance Chain:
Inherits: Data
  • Object
Defined in: ext/stringio/stringio.c

Overview

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"

Class Method Summary

Instance Attribute Summary

Instance Method Summary

::IO::generic_readable - Included

#read_nonblock

Similar to #read, but raises EOFError at end of string unless the exception: false option is passed in.

#readbyte

See IO#readbyte.

#readchar

See IO#readchar.

#readline

See IO#readline.

#readpartial
#sysread

Similar to #read, but raises EOFError at end of string instead of returning nil, as well as IO#sysread does.

::IO::generic_writable - Included

Constructor Details

.new(string=""[, mode])

Creates new StringIO instance from with string and mode.

Class Method Details

.open(string=""[, mode]) {|strio| ... }

Equivalent to .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 Attribute Details

#closed?Boolean (readonly)

Returns true if strio is completely closed, false otherwise.

#closed_read?Boolean (readonly)

Returns true if strio is not readable, false otherwise.

#closed_write?Boolean (readonly)

Returns true if strio is not writable, false otherwise.

#eofBoolean (readonly) #eof?Boolean
Also known as: #eof?

Returns true if strio is at end of file. The stringio must be opened for reading or an IOError will be raised.

#eofBoolean (readonly) #eof?Boolean

Alias for #eof.

#linenoInteger (rw)

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.

#lineno=(integer) ⇒ Integer (rw)

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

#posInteger (rw) #tellInteger

Returns the current offset (in bytes) of strio.

#pos=(integer) ⇒ Integer (rw)

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

#stringString (rw)

Returns underlying String object, the subject of ::IO.

#string=(string) ⇒ String (rw)

Changes underlying String object, the subject of ::IO.

#tty?Boolean (readonly)

Instance Method Details

#binmodetrue

#bytes

This is a deprecated alias for #each_byte.

#chars

This is a deprecated alias for #each_char.

#closenil

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

#close_readnil

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

#close_writenil

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

#codepoints

This is a deprecated alias for #each_codepoint.

#each(sep = $/) {|line| ... } ⇒ strio #each(limit) {|line| ... } ⇒ strio #each(sep, limit) {|line| ... } ⇒ strio #each(...) ⇒ Enumerator
Also known as: #each_line

strio.each_line(sep=$/) {|line| block } -> strio

strio.each_line(limit) {|line| block }     -> strio
strio.each_line(sep,limit) {|line| block } -> strio
strio.each_line(...)                       -> anEnumerator

See IO#each.

#each_byte {|byte| ... } ⇒ strio #each_byteEnumerator

See IO#each_byte.

#each_char {|char| ... } ⇒ strio #each_charEnumerator

See IO#each_char.

#each_codepoint {|c| ... } ⇒ strio #each_codepointEnumerator

See IO#each_codepoint.

#each(sep = $/) {|line| ... } ⇒ strio #each(limit) {|line| ... } ⇒ strio #each(sep, limit) {|line| ... } ⇒ strio #each(...) ⇒ Enumerator

Alias for #each.

#external_encodingEncoding

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

#fcntl

#fileno

#flush

#fsync

#getbyteFixnum?

See IO#getbyte.

#getcString?

See IO#getc.

#gets(sep = $/) ⇒ String? #gets(limit) ⇒ String? #gets(sep, limit) ⇒ String?

See IO#gets.

#internal_encodingEncoding

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

#isatty

#lengthInteger #sizeInteger
Also known as: #size

Returns the size of the buffer string.

#lines(*args)

This is a deprecated alias for #each_line.

#pid

#putc(obj) ⇒ Object

See IO#putc.

#read([length [, outbuf]]) ⇒ String, ...

See IO#read.

#readlines(sep = $/) ⇒ Array #readlines(limit) ⇒ Array #readlines(sep, limit) ⇒ Array

See IO#readlines.

#reopen(other_StrIO) ⇒ strio #reopen(string, mode) ⇒ strio

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

#rewind0

Positions strio to the beginning of input, resetting #lineno to zero.

#seek(amount, whence = SEEK_SET) ⇒ 0

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

#set_encoding(ext_enc, [int_enc[, opt]]) ⇒ strio

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.

#lengthInteger #sizeInteger

Alias for #length.

#synctrue

Returns true always.

#sync=

#tell

#truncate(integer) ⇒ 0

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

#ungetbyte(fixnum) ⇒ nil

See IO#ungetbyte

#ungetc(string) ⇒ nil

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.

#write(string) ⇒ Integer #syswrite(string) ⇒ Integer

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.