File: C:/Ruby27-x64/share/doc/ruby/html/StringIO.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>class StringIO - RDoc Documentation</title>
<script type="text/javascript">
var rdoc_rel_prefix = "./";
var index_rel_prefix = "./";
</script>
<script src="./js/navigation.js" defer></script>
<script src="./js/search.js" defer></script>
<script src="./js/search_index.js" defer></script>
<script src="./js/searcher.js" defer></script>
<script src="./js/darkfish.js" defer></script>
<link href="./css/fonts.css" rel="stylesheet">
<link href="./css/rdoc.css" rel="stylesheet">
<body id="top" role="document" class="class">
<nav role="navigation">
<div id="project-navigation">
<div id="home-section" role="region" title="Quick navigation" class="nav-section">
<h2>
<a href="./index.html" rel="home">Home</a>
</h2>
<div id="table-of-contents-navigation">
<a href="./table_of_contents.html#pages">Pages</a>
<a href="./table_of_contents.html#classes">Classes</a>
<a href="./table_of_contents.html#methods">Methods</a>
</div>
</div>
<div id="search-section" role="search" class="project-section initially-hidden">
<form action="#" method="get" accept-charset="utf-8">
<div id="search-field-wrapper">
<input id="search-field" role="combobox" aria-label="Search"
aria-autocomplete="list" aria-controls="search-results"
type="text" name="search" placeholder="Search" spellcheck="false"
title="Type to search, Up and Down to navigate, Enter to load">
</div>
<ul id="search-results" aria-label="Search Results"
aria-busy="false" aria-expanded="false"
aria-atomic="false" class="initially-hidden"></ul>
</form>
</div>
</div>
<div id="class-metadata">
<div id="parent-class-section" class="nav-section">
<h3>Parent</h3>
<p class="link"><a href="Data.html">Data</a>
</div>
<div id="includes-section" class="nav-section">
<h3>Included Modules</h3>
<ul class="link-list">
<li><a class="include" href="Enumerable.html">Enumerable</a>
<li><a class="include" href="IO/generic_readable.html">IO::generic_readable</a>
<li><a class="include" href="IO/generic_writable.html">IO::generic_writable</a>
</ul>
</div>
<!-- Method Quickref -->
<div id="method-list-section" class="nav-section">
<h3>Methods</h3>
<ul class="link-list" role="directory">
<li ><a href="#method-c-new">::new</a>
<li ><a href="#method-c-open">::open</a>
<li ><a href="#method-i-binmode">#binmode</a>
<li ><a href="#method-i-bytes">#bytes</a>
<li ><a href="#method-i-chars">#chars</a>
<li ><a href="#method-i-close">#close</a>
<li ><a href="#method-i-close_read">#close_read</a>
<li ><a href="#method-i-close_write">#close_write</a>
<li ><a href="#method-i-closed-3F">#closed?</a>
<li ><a href="#method-i-closed_read-3F">#closed_read?</a>
<li ><a href="#method-i-closed_write-3F">#closed_write?</a>
<li ><a href="#method-i-codepoints">#codepoints</a>
<li ><a href="#method-i-each">#each</a>
<li ><a href="#method-i-each_byte">#each_byte</a>
<li ><a href="#method-i-each_char">#each_char</a>
<li ><a href="#method-i-each_codepoint">#each_codepoint</a>
<li ><a href="#method-i-each_line">#each_line</a>
<li ><a href="#method-i-eof">#eof</a>
<li ><a href="#method-i-eof-3F">#eof?</a>
<li ><a href="#method-i-external_encoding">#external_encoding</a>
<li ><a href="#method-i-fcntl">#fcntl</a>
<li ><a href="#method-i-fileno">#fileno</a>
<li ><a href="#method-i-flush">#flush</a>
<li ><a href="#method-i-fsync">#fsync</a>
<li ><a href="#method-i-getbyte">#getbyte</a>
<li ><a href="#method-i-getc">#getc</a>
<li ><a href="#method-i-gets">#gets</a>
<li ><a href="#method-i-internal_encoding">#internal_encoding</a>
<li ><a href="#method-i-isatty">#isatty</a>
<li ><a href="#method-i-length">#length</a>
<li ><a href="#method-i-lineno">#lineno</a>
<li ><a href="#method-i-lineno-3D">#lineno=</a>
<li ><a href="#method-i-lines">#lines</a>
<li ><a href="#method-i-pid">#pid</a>
<li ><a href="#method-i-pos">#pos</a>
<li ><a href="#method-i-pos-3D">#pos=</a>
<li ><a href="#method-i-putc">#putc</a>
<li ><a href="#method-i-read">#read</a>
<li ><a href="#method-i-readlines">#readlines</a>
<li ><a href="#method-i-reopen">#reopen</a>
<li ><a href="#method-i-rewind">#rewind</a>
<li ><a href="#method-i-seek">#seek</a>
<li ><a href="#method-i-set_encoding">#set_encoding</a>
<li ><a href="#method-i-set_encoding_by_bom">#set_encoding_by_bom</a>
<li ><a href="#method-i-size">#size</a>
<li ><a href="#method-i-string">#string</a>
<li ><a href="#method-i-string-3D">#string=</a>
<li ><a href="#method-i-sync">#sync</a>
<li ><a href="#method-i-sync-3D">#sync=</a>
<li ><a href="#method-i-tell">#tell</a>
<li ><a href="#method-i-truncate">#truncate</a>
<li ><a href="#method-i-tty-3F">#tty?</a>
<li ><a href="#method-i-ungetbyte">#ungetbyte</a>
<li ><a href="#method-i-ungetc">#ungetc</a>
<li ><a href="#method-i-write">#write</a>
</ul>
</div>
</div>
</nav>
<main role="main" aria-labelledby="class-StringIO">
<h1 id="class-StringIO" class="class">
class StringIO
</h1>
<section class="description">
<p>Pseudo I/O on <a href="String.html"><code>String</code></a> object, with interface corresponding to <a href="IO.html"><code>IO</code></a>.</p>
<p>Commonly used to simulate <code>$stdio</code> or <code>$stderr</code></p>
<h3 id="class-StringIO-label-Examples">Examples<span><a href="#class-StringIO-label-Examples">¶</a> <a href="#top">↑</a></span></h3>
<pre class="ruby"><span class="ruby-identifier">require</span> <span class="ruby-string">'stringio'</span>
<span class="ruby-comment"># Writing stream emulation</span>
<span class="ruby-identifier">io</span> = <span class="ruby-constant">StringIO</span>.<span class="ruby-identifier">new</span>
<span class="ruby-identifier">io</span>.<span class="ruby-identifier">puts</span> <span class="ruby-string">"Hello World"</span>
<span class="ruby-identifier">io</span>.<span class="ruby-identifier">string</span> <span class="ruby-comment">#=> "Hello World\n"</span>
<span class="ruby-comment"># Reading stream emulation</span>
<span class="ruby-identifier">io</span> = <span class="ruby-constant">StringIO</span>.<span class="ruby-identifier">new</span> <span class="ruby-string">"first\nsecond\nlast\n"</span>
<span class="ruby-identifier">io</span>.<span class="ruby-identifier">getc</span> <span class="ruby-comment">#=> "f"</span>
<span class="ruby-identifier">io</span>.<span class="ruby-identifier">gets</span> <span class="ruby-comment">#=> "irst\n"</span>
<span class="ruby-identifier">io</span>.<span class="ruby-identifier">read</span> <span class="ruby-comment">#=> "second\nlast\n"</span>
</pre>
</section>
<section id="5Buntitled-5D" class="documentation-section">
<section class="constants-list">
<header>
<h3>Constants</h3>
</header>
<dl>
<dt id="VERSION">VERSION
<dd>
</dl>
</section>
<section id="public-class-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Class Methods</h3>
</header>
<div id="method-c-new" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
new(string=""[, mode])
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Creates new <a href="StringIO.html"><code>StringIO</code></a> instance from with <em>string</em> and <em>mode</em>.</p>
<div class="method-source-code" id="new-source">
<pre>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);
}</pre>
</div>
</div>
</div>
<div id="method-c-open" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
open(string=""[, mode]) {|strio| ...}
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Equivalent to <a href="StringIO.html#method-c-new"><code>StringIO.new</code></a> except that when it is called with a block, it yields with the new instance and closes it, and returns the result which returned from the block.</p>
<div class="method-source-code" id="open-source">
<pre>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);
}</pre>
</div>
</div>
</div>
</section>
<section id="public-instance-5Buntitled-5D-method-details" class="method-section">
<header>
<h3>Public Instance Methods</h3>
</header>
<div id="method-i-binmode" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
binmode → stringio
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Puts stream into binary mode. See <a href="IO.html#method-i-binmode"><code>IO#binmode</code></a>.</p>
<div class="method-source-code" id="binmode-source">
<pre>static VALUE
strio_binmode(VALUE self)
{
struct StringIO *ptr = StringIO(self);
rb_encoding *enc = rb_ascii8bit_encoding();
ptr->enc = enc;
if (WRITABLE(self)) {
rb_enc_associate(ptr->string, enc);
}
return self;
}</pre>
</div>
</div>
</div>
<div id="method-i-bytes" class="method-detail ">
<div class="method-heading">
<span class="method-name">bytes</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>This is a deprecated alias for <a href="StringIO.html#method-i-each_byte"><code>each_byte</code></a>.</p>
<div class="method-source-code" id="bytes-source">
<pre>static VALUE
strio_bytes(VALUE self)
{
rb_warn("StringIO#bytes is deprecated; use #each_byte instead");
if (!rb_block_given_p())
return rb_enumeratorize(self, ID2SYM(rb_intern("each_byte")), 0, 0);
return strio_each_byte(self);
}</pre>
</div>
</div>
</div>
<div id="method-i-chars" class="method-detail ">
<div class="method-heading">
<span class="method-name">chars</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>This is a deprecated alias for <a href="StringIO.html#method-i-each_char"><code>each_char</code></a>.</p>
<div class="method-source-code" id="chars-source">
<pre>static VALUE
strio_chars(VALUE self)
{
rb_warn("StringIO#chars is deprecated; use #each_char instead");
if (!rb_block_given_p())
return rb_enumeratorize(self, ID2SYM(rb_intern("each_char")), 0, 0);
return strio_each_char(self);
}</pre>
</div>
</div>
</div>
<div id="method-i-close" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
close → nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Closes a <a href="StringIO.html"><code>StringIO</code></a>. The stream is unavailable for any further data operations; an <code>IOError</code> is raised if such an attempt is made.</p>
<div class="method-source-code" id="close-source">
<pre>static VALUE
strio_close(VALUE self)
{
StringIO(self);
RBASIC(self)->flags &= ~STRIO_READWRITE;
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-i-close_read" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
close_read → nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Closes the read end of a <a href="StringIO.html"><code>StringIO</code></a>. Will raise an <code>IOError</code> if the receiver is not readable.</p>
<div class="method-source-code" id="close_read-source">
<pre>static VALUE
strio_close_read(VALUE self)
{
struct StringIO *ptr = StringIO(self);
if (!(ptr->flags & FMODE_READABLE)) {
rb_raise(rb_eIOError, "closing non-duplex IO for reading");
}
RBASIC(self)->flags &= ~STRIO_READABLE;
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-i-close_write" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
close_write → nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Closes the write end of a <a href="StringIO.html"><code>StringIO</code></a>. Will raise an <code>IOError</code> if the receiver is not writeable.</p>
<div class="method-source-code" id="close_write-source">
<pre>static VALUE
strio_close_write(VALUE self)
{
struct StringIO *ptr = StringIO(self);
if (!(ptr->flags & FMODE_WRITABLE)) {
rb_raise(rb_eIOError, "closing non-duplex IO for writing");
}
RBASIC(self)->flags &= ~STRIO_WRITABLE;
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-i-closed-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
closed? → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>true</code> if the stream is completely closed, <code>false</code> otherwise.</p>
<div class="method-source-code" id="closed-3F-source">
<pre>static VALUE
strio_closed(VALUE self)
{
StringIO(self);
if (!CLOSED(self)) return Qfalse;
return Qtrue;
}</pre>
</div>
</div>
</div>
<div id="method-i-closed_read-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
closed_read? → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>true</code> if the stream is not readable, <code>false</code> otherwise.</p>
<div class="method-source-code" id="closed_read-3F-source">
<pre>static VALUE
strio_closed_read(VALUE self)
{
StringIO(self);
if (READABLE(self)) return Qfalse;
return Qtrue;
}</pre>
</div>
</div>
</div>
<div id="method-i-closed_write-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
closed_write? → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>true</code> if the stream is not writable, <code>false</code> otherwise.</p>
<div class="method-source-code" id="closed_write-3F-source">
<pre>static VALUE
strio_closed_write(VALUE self)
{
StringIO(self);
if (WRITABLE(self)) return Qfalse;
return Qtrue;
}</pre>
</div>
</div>
</div>
<div id="method-i-codepoints" class="method-detail ">
<div class="method-heading">
<span class="method-name">codepoints</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>This is a deprecated alias for <a href="StringIO.html#method-i-each_codepoint"><code>each_codepoint</code></a>.</p>
<div class="method-source-code" id="codepoints-source">
<pre>static VALUE
strio_codepoints(VALUE self)
{
rb_warn("StringIO#codepoints is deprecated; use #each_codepoint instead");
if (!rb_block_given_p())
return rb_enumeratorize(self, ID2SYM(rb_intern("each_codepoint")), 0, 0);
return strio_each_codepoint(self);
}</pre>
</div>
</div>
</div>
<div id="method-i-each" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
each(sep=$/, chomp: false) {|line| block } → strio
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
each(limit, chomp: false) {|line| block } → strio
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
each(sep, limit, chomp: false) {|line| block } → strio
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
each(...) → anEnumerator
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
each_line(sep=$/, chomp: false) {|line| block } → strio
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
each_line(limit, chomp: false) {|line| block } → strio
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
each_line(sep, limit, chomp: false) {|line| block } → strio
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
each_line(...) → anEnumerator
</span>
</div>
<div class="method-description">
<p>See <a href="IO.html#method-i-each"><code>IO#each</code></a>.</p>
<div class="method-source-code" id="each-source">
<pre>static VALUE
strio_each(int argc, VALUE *argv, VALUE self)
{
VALUE line;
struct getline_arg arg;
StringIO(self);
RETURN_ENUMERATOR(self, argc, argv);
if (prepare_getline_args(&arg, argc, argv)->limit == 0) {
rb_raise(rb_eArgError, "invalid limit: 0 for each_line");
}
while (!NIL_P(line = strio_getline(&arg, readable(self)))) {
rb_yield(line);
}
return self;
}</pre>
</div>
</div>
</div>
<div id="method-i-each_byte" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
each_byte {|byte| block } → strio
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
each_byte → anEnumerator
</span>
</div>
<div class="method-description">
<p>See <a href="IO.html#method-i-each_byte"><code>IO#each_byte</code></a>.</p>
<div class="method-source-code" id="each_byte-source">
<pre>static VALUE
strio_each_byte(VALUE self)
{
struct StringIO *ptr = readable(self);
RETURN_ENUMERATOR(self, 0, 0);
while (ptr->pos < RSTRING_LEN(ptr->string)) {
char c = RSTRING_PTR(ptr->string)[ptr->pos++];
rb_yield(CHR2FIX(c));
}
return self;
}</pre>
</div>
</div>
</div>
<div id="method-i-each_char" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
each_char {|char| block } → strio
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
each_char → anEnumerator
</span>
</div>
<div class="method-description">
<p>See <a href="IO.html#method-i-each_char"><code>IO#each_char</code></a>.</p>
<div class="method-source-code" id="each_char-source">
<pre>static VALUE
strio_each_char(VALUE self)
{
VALUE c;
RETURN_ENUMERATOR(self, 0, 0);
while (!NIL_P(c = strio_getc(self))) {
rb_yield(c);
}
return self;
}</pre>
</div>
</div>
</div>
<div id="method-i-each_codepoint" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
each_codepoint {|c| block } → strio
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
each_codepoint → anEnumerator
</span>
</div>
<div class="method-description">
<p>See <a href="IO.html#method-i-each_codepoint"><code>IO#each_codepoint</code></a>.</p>
<div class="method-source-code" id="each_codepoint-source">
<pre>static VALUE
strio_each_codepoint(VALUE self)
{
struct StringIO *ptr;
rb_encoding *enc;
unsigned int c;
int n;
RETURN_ENUMERATOR(self, 0, 0);
ptr = readable(self);
enc = get_enc(ptr);
for (;;) {
if (ptr->pos >= RSTRING_LEN(ptr->string)) {
return self;
}
c = rb_enc_codepoint_len(RSTRING_PTR(ptr->string)+ptr->pos,
RSTRING_END(ptr->string), &n, enc);
rb_yield(UINT2NUM(c));
ptr->pos += n;
}
return self;
}</pre>
</div>
</div>
</div>
<div id="method-i-each_line" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
each_line(sep=$/, chomp: false) {|line| block } → strio
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
each_line(limit, chomp: false) {|line| block } → strio
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
each_line(sep, limit, chomp: false) {|line| block } → strio
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
each_line(...) → anEnumerator
</span>
</div>
<div class="method-description">
<p>See <a href="IO.html#method-i-each"><code>IO#each</code></a>.</p>
<div class="method-source-code" id="each_line-source">
<pre>static VALUE
strio_each(int argc, VALUE *argv, VALUE self)
{
VALUE line;
struct getline_arg arg;
StringIO(self);
RETURN_ENUMERATOR(self, argc, argv);
if (prepare_getline_args(&arg, argc, argv)->limit == 0) {
rb_raise(rb_eArgError, "invalid limit: 0 for each_line");
}
while (!NIL_P(line = strio_getline(&arg, readable(self)))) {
rb_yield(line);
}
return self;
}</pre>
</div>
</div>
</div>
<div id="method-i-eof" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
eof → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
eof? → true or false
</span>
</div>
<div class="method-description">
<p>Returns true if the stream is at the end of the data (underlying string). The stream must be opened for reading or an <code>IOError</code> will be raised.</p>
<div class="method-source-code" id="eof-source">
<pre>static VALUE
strio_eof(VALUE self)
{
struct StringIO *ptr = readable(self);
if (ptr->pos < RSTRING_LEN(ptr->string)) return Qfalse;
return Qtrue;
}</pre>
</div>
</div>
</div>
<div id="method-i-eof-3F" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
eof? → true or false
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns true if the stream is at the end of the data (underlying string). The stream must be opened for reading or an <code>IOError</code> will be raised.</p>
<div class="method-source-code" id="eof-3F-source">
<pre>static VALUE
strio_eof(VALUE self)
{
struct StringIO *ptr = readable(self);
if (ptr->pos < RSTRING_LEN(ptr->string)) return Qfalse;
return Qtrue;
}</pre>
</div>
</div>
</div>
<div id="method-i-external_encoding" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
external_encoding → encoding
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the <a href="Encoding.html"><code>Encoding</code></a> object that represents the encoding of the file. If the stream is write mode and no encoding is specified, returns <code>nil</code>.</p>
<div class="method-source-code" id="external_encoding-source">
<pre>static VALUE
strio_external_encoding(VALUE self)
{
struct StringIO *ptr = StringIO(self);
return rb_enc_from_encoding(get_enc(ptr));
}</pre>
</div>
</div>
</div>
<div id="method-i-fcntl" class="method-detail ">
<div class="method-heading">
<span class="method-name">fcntl</span><span
class="method-args">(*args)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Raises <a href="NotImplementedError.html"><code>NotImplementedError</code></a>.</p>
<div class="method-source-code" id="fcntl-source">
<pre>static VALUE
strio_unimpl(int argc, VALUE *argv, VALUE self)
{
StringIO(self);
rb_notimplement();
UNREACHABLE;
}</pre>
</div>
</div>
</div>
<div id="method-i-fileno" class="method-detail ">
<div class="method-heading">
<span class="method-name">fileno</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>nil</code>. Just for compatibility to <a href="IO.html"><code>IO</code></a>.</p>
<div class="method-source-code" id="fileno-source">
<pre>static VALUE
strio_nil(VALUE self)
{
StringIO(self);
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-i-flush" class="method-detail ">
<div class="method-heading">
<span class="method-name">flush</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns an object itself. Just for compatibility to <a href="IO.html"><code>IO</code></a>.</p>
<div class="method-source-code" id="flush-source">
<pre>static VALUE
strio_self(VALUE self)
{
StringIO(self);
return self;
}</pre>
</div>
</div>
</div>
<div id="method-i-fsync" class="method-detail ">
<div class="method-heading">
<span class="method-name">fsync</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns 0. Just for compatibility to <a href="IO.html"><code>IO</code></a>.</p>
<div class="method-source-code" id="fsync-source">
<pre>static VALUE
strio_0(VALUE self)
{
StringIO(self);
return INT2FIX(0);
}</pre>
</div>
</div>
</div>
<div id="method-i-getbyte" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
getbyte → fixnum or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>See <a href="IO.html#method-i-getbyte"><code>IO#getbyte</code></a>.</p>
<div class="method-source-code" id="getbyte-source">
<pre>static VALUE
strio_getbyte(VALUE self)
{
struct StringIO *ptr = readable(self);
int c;
if (ptr->pos >= RSTRING_LEN(ptr->string)) {
return Qnil;
}
c = RSTRING_PTR(ptr->string)[ptr->pos++];
return CHR2FIX(c);
}</pre>
</div>
</div>
</div>
<div id="method-i-getc" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
getc → string or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>See <a href="IO.html#method-i-getc"><code>IO#getc</code></a>.</p>
<div class="method-source-code" id="getc-source">
<pre>static VALUE
strio_getc(VALUE self)
{
struct StringIO *ptr = readable(self);
rb_encoding *enc = get_enc(ptr);
VALUE str = ptr->string;
long pos = ptr->pos;
int len;
char *p;
if (pos >= RSTRING_LEN(str)) {
return Qnil;
}
p = RSTRING_PTR(str)+pos;
len = rb_enc_mbclen(p, RSTRING_END(str), enc);
ptr->pos += len;
return enc_subseq(str, pos, len, enc);
}</pre>
</div>
</div>
</div>
<div id="method-i-gets" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
gets(sep=$/, chomp: false) → string or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
gets(limit, chomp: false) → string or nil
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
gets(sep, limit, chomp: false) → string or nil
</span>
</div>
<div class="method-description">
<p>See <a href="IO.html#method-i-gets"><code>IO#gets</code></a>.</p>
<div class="method-source-code" id="gets-source">
<pre>static VALUE
strio_gets(int argc, VALUE *argv, VALUE self)
{
struct getline_arg arg;
VALUE str;
if (prepare_getline_args(&arg, argc, argv)->limit == 0) {
struct StringIO *ptr = readable(self);
return rb_enc_str_new(0, 0, get_enc(ptr));
}
str = strio_getline(&arg, readable(self));
rb_lastline_set(str);
return str;
}</pre>
</div>
</div>
</div>
<div id="method-i-internal_encoding" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
internal_encoding → encoding
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the <a href="Encoding.html"><code>Encoding</code></a> of the internal string if conversion is specified. Otherwise returns <code>nil</code>.</p>
<div class="method-source-code" id="internal_encoding-source">
<pre>static VALUE
strio_internal_encoding(VALUE self)
{
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-i-isatty" class="method-detail ">
<div class="method-heading">
<span class="method-name">isatty</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>false</code>. Just for compatibility to <a href="IO.html"><code>IO</code></a>.</p>
<div class="method-source-code" id="isatty-source">
<pre>static VALUE
strio_false(VALUE self)
{
StringIO(self);
return Qfalse;
}</pre>
</div>
</div>
</div>
<div id="method-i-length" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
length → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the size of the buffer string.</p>
<div class="method-source-code" id="length-source">
<pre>static VALUE
strio_size(VALUE self)
{
VALUE string = StringIO(self)->string;
if (NIL_P(string)) {
rb_raise(rb_eIOError, "not opened");
}
return ULONG2NUM(RSTRING_LEN(string));
}</pre>
</div>
</div>
</div>
<div id="method-i-lineno" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
lineno → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the current line number. The stream must be opened for reading. <code>lineno</code> counts the number of times <code>gets</code> is called, rather than the number of newlines encountered. The two values will differ if <code>gets</code> is called with a separator other than newline. See also the <code>$.</code> variable.</p>
<div class="method-source-code" id="lineno-source">
<pre>static VALUE
strio_get_lineno(VALUE self)
{
return LONG2NUM(StringIO(self)->lineno);
}</pre>
</div>
</div>
</div>
<div id="method-i-lineno-3D" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
lineno = integer → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Manually sets the current line number to the given value. <code>$.</code> is updated only on the next read.</p>
<div class="method-source-code" id="lineno-3D-source">
<pre>static VALUE
strio_set_lineno(VALUE self, VALUE lineno)
{
StringIO(self)->lineno = NUM2LONG(lineno);
return lineno;
}</pre>
</div>
</div>
</div>
<div id="method-i-lines" class="method-detail ">
<div class="method-heading">
<span class="method-name">lines</span><span
class="method-args">(*args)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>This is a deprecated alias for <a href="StringIO.html#method-i-each_line"><code>each_line</code></a>.</p>
<div class="method-source-code" id="lines-source">
<pre>static VALUE
strio_lines(int argc, VALUE *argv, VALUE self)
{
rb_warn("StringIO#lines is deprecated; use #each_line instead");
if (!rb_block_given_p())
return rb_enumeratorize(self, ID2SYM(rb_intern("each_line")), argc, argv);
return strio_each(argc, argv, self);
}</pre>
</div>
</div>
</div>
<div id="method-i-pid" class="method-detail ">
<div class="method-heading">
<span class="method-name">pid</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>nil</code>. Just for compatibility to <a href="IO.html"><code>IO</code></a>.</p>
<div class="method-source-code" id="pid-source">
<pre>static VALUE
strio_nil(VALUE self)
{
StringIO(self);
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-i-pos" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
pos → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
tell → integer
</span>
</div>
<div class="method-description">
<p>Returns the current offset (in bytes).</p>
<div class="method-source-code" id="pos-source">
<pre>static VALUE
strio_get_pos(VALUE self)
{
return LONG2NUM(StringIO(self)->pos);
}</pre>
</div>
</div>
</div>
<div id="method-i-pos-3D" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
pos = integer → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Seeks to the given position (in bytes).</p>
<div class="method-source-code" id="pos-3D-source">
<pre>static VALUE
strio_set_pos(VALUE self, VALUE pos)
{
struct StringIO *ptr = StringIO(self);
long p = NUM2LONG(pos);
if (p < 0) {
error_inval(0);
}
ptr->pos = p;
return pos;
}</pre>
</div>
</div>
</div>
<div id="method-i-putc" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
putc(obj) → obj
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>See <a href="IO.html#method-i-putc"><code>IO#putc</code></a>.</p>
<div class="method-source-code" id="putc-source">
<pre>static VALUE
strio_putc(VALUE self, VALUE ch)
{
struct StringIO *ptr = writable(self);
VALUE str;
check_modifiable(ptr);
if (RB_TYPE_P(ch, T_STRING)) {
str = rb_str_substr(ch, 0, 1);
}
else {
char c = NUM2CHR(ch);
str = rb_str_new(&c, 1);
}
strio_write(self, str);
return ch;
}</pre>
</div>
</div>
</div>
<div id="method-i-read" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
read([length [, outbuf]]) → string, outbuf, or nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>See <a href="IO.html#method-i-read"><code>IO#read</code></a>.</p>
<div class="method-source-code" id="read-source">
<pre>static VALUE
strio_read(int argc, VALUE *argv, VALUE self)
{
struct StringIO *ptr = readable(self);
VALUE str = Qnil;
long len;
int binary = 0;
rb_check_arity(argc, 0, 2);
switch (argc) {
case 2:
str = argv[1];
if (!NIL_P(str)) {
StringValue(str);
rb_str_modify(str);
}
/* fall through */
case 1:
if (!NIL_P(argv[0])) {
len = NUM2LONG(argv[0]);
if (len < 0) {
rb_raise(rb_eArgError, "negative length %ld given", len);
}
if (len > 0 && ptr->pos >= RSTRING_LEN(ptr->string)) {
if (!NIL_P(str)) rb_str_resize(str, 0);
return Qnil;
}
binary = 1;
break;
}
/* fall through */
case 0:
len = RSTRING_LEN(ptr->string);
if (len <= ptr->pos) {
rb_encoding *enc = get_enc(ptr);
if (NIL_P(str)) {
str = rb_str_new(0, 0);
}
else {
rb_str_resize(str, 0);
}
rb_enc_associate(str, enc);
return str;
}
else {
len -= ptr->pos;
}
break;
}
if (NIL_P(str)) {
rb_encoding *enc = binary ? rb_ascii8bit_encoding() : get_enc(ptr);
str = strio_substr(ptr, ptr->pos, len, enc);
}
else {
long rest = RSTRING_LEN(ptr->string) - ptr->pos;
if (len > rest) len = rest;
rb_str_resize(str, len);
MEMCPY(RSTRING_PTR(str), RSTRING_PTR(ptr->string) + ptr->pos, char, len);
if (binary)
rb_enc_associate(str, rb_ascii8bit_encoding());
else
rb_enc_copy(str, ptr->string);
}
ptr->pos += RSTRING_LEN(str);
return str;
}</pre>
</div>
</div>
</div>
<div id="method-i-readlines" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
readlines(sep=$/, chomp: false) → array
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
readlines(limit, chomp: false) → array
</span>
</div>
<div class="method-heading">
<span class="method-callseq">
readlines(sep, limit, chomp: false) → array
</span>
</div>
<div class="method-description">
<p>See <a href="IO.html#method-i-readlines"><code>IO#readlines</code></a>.</p>
<div class="method-source-code" id="readlines-source">
<pre>static VALUE
strio_readlines(int argc, VALUE *argv, VALUE self)
{
VALUE ary, line;
struct getline_arg arg;
StringIO(self);
ary = rb_ary_new();
if (prepare_getline_args(&arg, argc, argv)->limit == 0) {
rb_raise(rb_eArgError, "invalid limit: 0 for readlines");
}
while (!NIL_P(line = strio_getline(&arg, readable(self)))) {
rb_ary_push(ary, line);
}
return ary;
}</pre>
</div>
</div>
</div>
<div id="method-i-reopen" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
reopen(other_StrIO) → strio
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
reopen(string, mode) → strio
</span>
</div>
<div class="method-description">
<p>Reinitializes the stream with the given <em>other_StrIO</em> or <em>string</em> and <em>mode</em> (see StringIO#new).</p>
<div class="method-source-code" id="reopen-source">
<pre>static VALUE
strio_reopen(int argc, VALUE *argv, VALUE self)
{
rb_io_taint_check(self);
if (argc == 1 && !RB_TYPE_P(*argv, T_STRING)) {
return strio_copy(self, *argv);
}
return strio_init(argc, argv, StringIO(self), self);
}</pre>
</div>
</div>
</div>
<div id="method-i-rewind" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
rewind → 0
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Positions the stream to the beginning of input, resetting <code>lineno</code> to zero.</p>
<div class="method-source-code" id="rewind-source">
<pre>static VALUE
strio_rewind(VALUE self)
{
struct StringIO *ptr = StringIO(self);
ptr->pos = 0;
ptr->lineno = 0;
return INT2FIX(0);
}</pre>
</div>
</div>
</div>
<div id="method-i-seek" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
seek(amount, whence=SEEK_SET) → 0
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Seeks to a given offset <em>amount</em> in the stream according to the value of <em>whence</em> (see <a href="IO.html#method-i-seek"><code>IO#seek</code></a>).</p>
<div class="method-source-code" id="seek-source">
<pre>static VALUE
strio_seek(int argc, VALUE *argv, VALUE self)
{
VALUE whence;
struct StringIO *ptr = StringIO(self);
long amount, offset;
rb_scan_args(argc, argv, "11", NULL, &whence);
amount = NUM2LONG(argv[0]);
if (CLOSED(self)) {
rb_raise(rb_eIOError, "closed stream");
}
switch (NIL_P(whence) ? 0 : NUM2LONG(whence)) {
case 0:
offset = 0;
break;
case 1:
offset = ptr->pos;
break;
case 2:
offset = RSTRING_LEN(ptr->string);
break;
default:
error_inval("invalid whence");
}
if (amount > LONG_MAX - offset || amount + offset < 0) {
error_inval(0);
}
ptr->pos = amount + offset;
return INT2FIX(0);
}</pre>
</div>
</div>
</div>
<div id="method-i-set_encoding" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
set_encoding(ext_enc, [int_enc[, opt]]) → strio
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Specify the encoding of the <a href="StringIO.html"><code>StringIO</code></a> as <em>ext_enc</em>. Use the default external encoding if <em>ext_enc</em> is nil. 2nd argument <em>int_enc</em> and optional hash <em>opt</em> argument are ignored; they are for API compatibility to <a href="IO.html"><code>IO</code></a>.</p>
<div class="method-source-code" id="set_encoding-source">
<pre>static VALUE
strio_set_encoding(int argc, VALUE *argv, VALUE self)
{
rb_encoding* enc;
struct StringIO *ptr = StringIO(self);
VALUE ext_enc, int_enc, opt;
argc = rb_scan_args(argc, argv, "11:", &ext_enc, &int_enc, &opt);
if (NIL_P(ext_enc)) {
enc = rb_default_external_encoding();
}
else {
enc = rb_to_encoding(ext_enc);
}
ptr->enc = enc;
if (WRITABLE(self)) {
rb_enc_associate(ptr->string, enc);
}
return self;
}</pre>
</div>
</div>
</div>
<div id="method-i-set_encoding_by_bom" class="method-detail ">
<div class="method-heading">
<span class="method-name">set_encoding_by_bom</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<div class="method-source-code" id="set_encoding_by_bom-source">
<pre>static VALUE
strio_set_encoding_by_bom(VALUE self)
{
struct StringIO *ptr = StringIO(self);
if (!set_encoding_by_bom(ptr)) return Qnil;
return rb_enc_from_encoding(ptr->enc);
}</pre>
</div>
</div>
</div>
<div id="method-i-size" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
size → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the size of the buffer string.</p>
<div class="method-source-code" id="size-source">
<pre>static VALUE
strio_size(VALUE self)
{
VALUE string = StringIO(self)->string;
if (NIL_P(string)) {
rb_raise(rb_eIOError, "not opened");
}
return ULONG2NUM(RSTRING_LEN(string));
}</pre>
</div>
</div>
</div>
<div id="method-i-string" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
string → string
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns underlying <a href="String.html"><code>String</code></a> object, the subject of <a href="IO.html"><code>IO</code></a>.</p>
<div class="method-source-code" id="string-source">
<pre>static VALUE
strio_get_string(VALUE self)
{
return StringIO(self)->string;
}</pre>
</div>
</div>
</div>
<div id="method-i-string-3D" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
string = string → string
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Changes underlying <a href="String.html"><code>String</code></a> object, the subject of <a href="IO.html"><code>IO</code></a>.</p>
<div class="method-source-code" id="string-3D-source">
<pre>static VALUE
strio_set_string(VALUE self, VALUE string)
{
struct StringIO *ptr = StringIO(self);
rb_io_taint_check(self);
ptr->flags &= ~FMODE_READWRITE;
StringValue(string);
ptr->flags = OBJ_FROZEN(string) ? FMODE_READABLE : FMODE_READWRITE;
ptr->pos = 0;
ptr->lineno = 0;
return ptr->string = string;
}</pre>
</div>
</div>
</div>
<div id="method-i-sync" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
sync → true
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>true</code> always.</p>
<div class="method-source-code" id="sync-source">
<pre>static VALUE
strio_get_sync(VALUE self)
{
StringIO(self);
return Qtrue;
}</pre>
</div>
</div>
</div>
<div id="method-i-sync-3D" class="method-detail ">
<div class="method-heading">
<span class="method-name">sync=</span><span
class="method-args">(p1)</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns the argument unchanged. Just for compatibility to <a href="IO.html"><code>IO</code></a>.</p>
<div class="method-source-code" id="sync-3D-source">
<pre>static VALUE
strio_first(VALUE self, VALUE arg)
{
StringIO(self);
return arg;
}</pre>
</div>
</div>
</div>
<div id="method-i-tell" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
pos → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
tell → integer
</span>
</div>
<div class="method-description">
<p>Returns the current offset (in bytes).</p>
<div class="method-source-code" id="tell-source">
<pre>static VALUE
strio_get_pos(VALUE self)
{
return LONG2NUM(StringIO(self)->pos);
}</pre>
</div>
</div>
</div>
<div id="method-i-truncate" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
truncate(integer) → 0
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Truncates the buffer string to at most <em>integer</em> bytes. The stream must be opened for writing.</p>
<div class="method-source-code" id="truncate-source">
<pre>static VALUE
strio_truncate(VALUE self, VALUE len)
{
VALUE string = writable(self)->string;
long l = NUM2LONG(len);
long plen = RSTRING_LEN(string);
if (l < 0) {
error_inval("negative length");
}
rb_str_resize(string, l);
if (plen < l) {
MEMZERO(RSTRING_PTR(string) + plen, char, l - plen);
}
return len;
}</pre>
</div>
</div>
</div>
<div id="method-i-tty-3F" class="method-detail ">
<div class="method-heading">
<span class="method-name">tty?</span><span
class="method-args">()</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Returns <code>false</code>. Just for compatibility to <a href="IO.html"><code>IO</code></a>.</p>
<div class="method-source-code" id="tty-3F-source">
<pre>static VALUE
strio_false(VALUE self)
{
StringIO(self);
return Qfalse;
}</pre>
</div>
</div>
</div>
<div id="method-i-ungetbyte" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
ungetbyte(fixnum) → nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>See <a href="IO.html#method-i-ungetbyte"><code>IO#ungetbyte</code></a></p>
<div class="method-source-code" id="ungetbyte-source">
<pre>static VALUE
strio_ungetbyte(VALUE self, VALUE c)
{
struct StringIO *ptr = readable(self);
check_modifiable(ptr);
if (NIL_P(c)) return Qnil;
if (RB_INTEGER_TYPE_P(c)) {
/* rb_int_and() not visible from exts */
VALUE v = rb_funcall(c, '&', 1, INT2FIX(0xff));
const char cc = NUM2INT(v) & 0xFF;
strio_unget_bytes(ptr, &cc, 1);
}
else {
long cl;
SafeStringValue(c);
cl = RSTRING_LEN(c);
if (cl > 0) {
strio_unget_bytes(ptr, RSTRING_PTR(c), cl);
RB_GC_GUARD(c);
}
}
return Qnil;
}</pre>
</div>
</div>
</div>
<div id="method-i-ungetc" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
ungetc(string) → nil
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-description">
<p>Pushes back one character (passed as a parameter) such that a subsequent buffered read will return it. There is no limitation for multiple pushbacks including pushing back behind the beginning of the buffer string.</p>
<div class="method-source-code" id="ungetc-source">
<pre>static VALUE
strio_ungetc(VALUE self, VALUE c)
{
struct StringIO *ptr = readable(self);
rb_encoding *enc, *enc2;
check_modifiable(ptr);
if (NIL_P(c)) return Qnil;
if (RB_INTEGER_TYPE_P(c)) {
int len, cc = NUM2INT(c);
char buf[16];
enc = rb_enc_get(ptr->string);
len = rb_enc_codelen(cc, enc);
if (len <= 0) rb_enc_uint_chr(cc, enc);
rb_enc_mbcput(cc, buf, enc);
return strio_unget_bytes(ptr, buf, len);
}
else {
SafeStringValue(c);
enc = rb_enc_get(ptr->string);
enc2 = rb_enc_get(c);
if (enc != enc2 && enc != rb_ascii8bit_encoding()) {
c = rb_str_conv_enc(c, enc2, enc);
}
strio_unget_bytes(ptr, RSTRING_PTR(c), RSTRING_LEN(c));
RB_GC_GUARD(c);
return Qnil;
}
}</pre>
</div>
</div>
</div>
<div id="method-i-write" class="method-detail ">
<div class="method-heading">
<span class="method-callseq">
write(string, ...) → integer
</span>
<span class="method-click-advice">click to toggle source</span>
</div>
<div class="method-heading">
<span class="method-callseq">
syswrite(string) → integer
</span>
</div>
<div class="method-description">
<p>Appends the given string to the underlying buffer string. The stream must be opened for writing. If the argument is not a string, it will be converted to a string using <code>to_s</code>. Returns the number of bytes written. See <a href="IO.html#method-i-write"><code>IO#write</code></a>.</p>
<div class="method-source-code" id="write-source">
<pre>static VALUE
strio_write_m(int argc, VALUE *argv, VALUE self)
{
long len = 0;
while (argc-- > 0) {
/* StringIO can't exceed long limit */
len += strio_write(self, *argv++);
}
return LONG2NUM(len);
}</pre>
</div>
</div>
</div>
</section>
</section>
</main>
<footer id="validator-badges" role="contentinfo">
<p><a href="https://validator.w3.org/check/referer">Validate</a>
<p>Generated by <a href="https://ruby.github.io/rdoc/">RDoc</a> 6.2.1.1.
<p>Based on <a href="http://deveiate.org/projects/Darkfish-RDoc/">Darkfish</a> by <a href="http://deveiate.org">Michael Granger</a>.
</footer>