I recently read a nice post on using StringIO
in Ruby. What the author doesn't mention, though, is that StringIO
is just an "I." There's no "O." You can't do this, for example:
s = StringIO.new
s << 'foo'
s << 'bar'
s.to_s
# => should be "foo\nbar"
# => really is ''`
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.
Is there a good replacement?
@Mike Stone
It's true that Strings in Ruby are mutable, but that doesn't mean we should always rely on that functionality. If stuff
is large, the performance and memory requirements of this, for example, suck like Mega Maid:
result = stuff.map(&:to_s).join(' ')
The "correct" way to do this in Java is:
result = StringBuffer.new("")
for(String s : stuff) {
result.append(s);
}
Though my Java is a bit rusty.
@Mike Stone #2: Whoah!!!!! How did I miss that in the API? I'm still not sure of the performance of StringIO, but at least it's now "Ruby's" problem, not mine :)