title: Why doesn't Ruby have a real StringBuffer or StringIO?
body: <p>I recently read a nice <a href="http://macdevelopertips.com/ruby/using-a-string-as-a-file-in-ruby.html" rel="noreferrer">post</a> on using <code>StringIO</code> in Ruby.  What the author doesn't mention, though, is that <code>StringIO</code> is just an "I."  There's no "O."  You <em>can't</em> do this, for example:</p>
  
  <pre><code>s = StringIO.new
  s &lt;&lt; 'foo'
  s &lt;&lt; 'bar'
  s.to_s
  # =&gt; should be "foo\nbar"
  # =&gt; really is ''`
  </code></pre>
  
  <p>Ruby really needs a StringBuffer just like the one Java has. StringBuffers serve two important purposes. First, they let you test the output half of what Ruby's StringIO does. Second, they are useful for building up long strings from small parts -- something that Joel reminds us over and over again is otherwise very very slow.</p>
  
  <p>Is there a good replacement?</p>
  
  <p>It's true that Strings in Ruby are mutable, but that doesn't mean we should always rely on that functionality.  If <code>stuff</code> is large, the performance and memory requirements of this, for example, is really bad.</p>
  
  <pre><code>result = stuff.map(&amp;:to_s).join(' ')
  </code></pre>
  
  <p>The "correct" way to do this in Java is:</p>
  
  <pre><code>result = StringBuffer.new("")
  for(String s : stuff) {
    result.append(s);
  }
  </code></pre>
  
  <p>Though my Java is a bit rusty.</p>