class method
StringIO.new
(?::String string, ?::String? mode) -> ::StringIO
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);
}
Returns a new StringIO instance formed from string and mode; the instance should be closed when no longer needed:
Example
strio = StringIO.new strio.string # => "" strio.closed_read? # => false strio.closed_write? # => false strio.close
If string is frozen, the default mode is 'r':
Example
strio = StringIO.new('foo'.freeze) strio.string # => "foo" strio.closed_read? # => false strio.closed_write? # => true strio.close
Argument mode must be a valid Access Mode, which may be a string or an integer constant:
Example
StringIO.new('foo', 'w+') StringIO.new('foo', File::RDONLY)
Related: StringIO.open (passes the StringIO object to the block; closes the object automatically on block exit).