Skip to content
class method

StringIO.new

(?::String string, ?::String? mode) -> ::StringIO
StringIO.new(string = '', mode = 'r+') → new_stringio

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

Example
strio = StringIO.new
strio.string        # => ""
strio.closed_read?  # => false
strio.closed_write? # => false
strio.close

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

Example
strio = StringIO.new('foo'.freeze)
strio.string        # => "foo"
strio.closed_read?  # => false
strio.closed_write? # => true
strio.close

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

Example
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).

See also .open #binmode also in 4.1 · 3.4 · 3.3 · 3.2 · 3.1 · 3.0