Maintenance Ruby 3.3 gets fixes, but no new features. Switch to Ruby 4.0 →
class method
StringIO.new
StringIO.new(string = '', mode = 'r+') → new_stringio
ext/stringio/stringio.c
View on GitHub ↗
static VALUE
strio_initialize(int argc, VALUE *argv, VALUE self)
{
struct StringIO *ptr = check_strio(self);
if (!ptr) {
DATA_PTR(self) = ptr = strio_alloc();
}
rb_call_super(0, 0);
return strio_init(argc, argv, ptr, self);
}
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).