End of Life Ruby 3.2 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →
class method
StringIO.open
StringIO.open(string = '', mode = 'r+') {|strio| ... }
ext/stringio/stringio.c
View on GitHub ↗
static VALUE
strio_s_open(int argc, VALUE *argv, VALUE klass)
{
VALUE obj = rb_class_new_instance_kw(argc, argv, klass, RB_PASS_CALLED_KEYWORDS);
if (!rb_block_given_p()) return obj;
return rb_ensure(rb_yield, obj, strio_finalize, obj);
}
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.