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
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);
}
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.