Skip to content

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

class method

StringIO.open

[U] (?::String string, ?::String? mode) { (::StringIO arg) -> U } -> U
StringIO.open(string = '', mode = 'r+') {|strio| ... }

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

Creates a new StringIO instance formed from string and mode; see Access Modes.

With no block, returns the new instance:

Example
strio = StringIO.open # => #<StringIO>

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

Example
StringIO.open {|strio| p strio }
# => #<StringIO>

Related: StringIO.new.

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