Skip to content

Maintenance Ruby 3.4 gets fixes, but no new features. Switch to Ruby 4.0 →

class method

StringIO.new

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

Note that mode defaults to 'r' if string is frozen.

Returns a new StringIO instance formed from string and mode; see Access Modes:

Example
strio = StringIO.new # => #<StringIO>
strio.close

The instance should be closed when no longer needed.

Related: StringIO.open (accepts block; closes automatically).

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