Skip to content

Preview Ruby 4.1 is a preview. It can change before the final release. Switch to Ruby 4.0 →

class method

StringIO.open

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

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

With no block given, returns the new instance:

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

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

Example
StringIO.open('foo') {|strio| strio.string.upcase } # => "FOO"

Related: StringIO.new.

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