rubyapi.org

Class

Constants

Maximum length that a StringIO instance can hold

The version string

Class Methods

Returns a new StringIO instance formed from string and mode; the instance should be closed when no longer needed:

strio = StringIO.new
strio.string
strio.closed_read?
strio.closed_write?
strio.close

If string is frozen, the default mode is 'r':

strio = StringIO.new('foo'.freeze)
strio.string
strio.closed_read?
strio.closed_write?
strio.close

Argument mode must be a valid Access Mode, which may be a string or an integer constant:

StringIO.new('foo', 'w+')
StringIO.new('foo', File::RDONLY)

Related: StringIO.open (passes the StringIO object to the block; closes the object automatically on block exit).

Creates new StringIO instance by calling StringIO.new(string, mode).

With no block given, returns the new instance:

strio = StringIO.open

With a block given, calls the block with the new instance and returns the block’s value; closes the instance on block exit:

StringIO.open('foo') {|strio| strio.string.upcase }

Related: StringIO.new.

Instance Methods

Sets the data mode in self to binary mode; see Data Mode.

Closes self for reading; closed-write setting remains unchanged; returns nil:

strio = StringIO.new
strio.closed_read?
strio.close_read
strio.closed_read?
strio.closed_write?
strio.read

Related: StringIO#close, StringIO#close_write.

No documentation available

Returns whether self is positioned at end-of-stream:

strio = StringIO.new('foo')
strio.pos
strio.eof?
strio.read
strio.pos
strio.eof?
strio.close_read
strio.eof?

Related: StringIO#pos.

Returns an Encoding object that represents the encoding of the string; see Encodings:

strio = StringIO.new('foo')
strio.external_encoding

Returns nil if self has no string and is in write mode:

strio = StringIO.new(nil, 'w+')
strio.external_encoding

Returns nil; for compatibility with IO.

Returns self; for compatibility with IO.

Returns 0; for compatibility with IO.

No documentation available

No documentation available

No documentation available

Returns nil; for compatibility with IO.

Returns false; for compatibility with IO.

Returns the current line number in self; see Line Number.

Sets the current line number in self to the given new_line_number; see Line Number.

Returns nil; for compatibility with IO.

Returns the current position (in bytes); see Position.

Sets the current position (in bytes); see Position.

Reinitializes the stream with the given other (string or StringIO) and mode; see IO.new:

StringIO.open('foo') do |strio|
  p strio.string
  strio.reopen('bar')
  p strio.string
  other_strio = StringIO.new('baz')
  strio.reopen(other_strio)
  p strio.string
  other_strio.close
end

Output:

"foo"
"bar"
"baz"

Sets the position to the given integer offset (in bytes), with respect to a given constant 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.

Sets the encoding according to the BOM (Byte Order Mark) in the string.

Returns self if the BOM is found, otherwise +nil.

No documentation available

Returns underlying string:

StringIO.open('foo') do |strio|
  p strio.string
  strio.string = 'bar'
  p strio.string
end

Output:

"foo"
"bar"

Related: StringIO#string= (assigns the underlying string).

Replaces the stored string with other_string, and sets the position to zero; returns other_string:

StringIO.open('foo') do |strio|
  p strio.string
  strio.string = 'bar'
  p strio.string
end

Output:

"foo"
"bar"

Related: StringIO#string (returns the stored string).

Returns true; implemented only for compatibility with other stream classes.

Returns the argument unchanged. Just for compatibility to IO.

Returns the current position (in bytes); see Position.

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

Pushes back (“unshifts”) an 8-bit byte onto the stream; see Byte IO.

Pushes back (“unshifts”) a character or integer onto the stream; see Character IO.

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.

Read the original on rubyapi.org ↗