views:

2834

answers:

4

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 :)

+1  A: 

Well, a StringBuffer is not quite as necessary in Ruby, mainly because Strings in Ruby are mutable... thus you can build up a string by modifying the existing string instead of constructing new strings with each concat.

As a note, you can also use special string syntax where you can build a string which references other variables within the string, which makes for very readable string construction. Consider:

first = "Mike"
last = "Stone"
name = "#{first} #{last}"

These strings can also contain expressions, not just variables... such as:

str = "The count will be: #{count + 1}"
count = count + 1
Mike Stone
That's certainly true, and it's great for short interpolations. It's lousy for building long strings like HTML pages, though. See http://en.wikipedia.org/wiki/Schlemiel_the_painter%27s_Algorithm
James A. Rosen
+20  A: 

I looked at the ruby documentation for StringIO, and it looks like what you want is strio.string, not strio.to_s

Thus, change your code to:

s = StringIO.new
s << 'foo'
s << 'bar'
s.string
Mike Stone
+3  A: 

Your example works in Ruby - I just tried it.

irb(main):001:0> require 'stringio'
=> true
irb(main):002:0> s = StringIO.new
=> #<StringIO:0x2ced9a0>
irb(main):003:0> s << 'foo'
=> #<StringIO:0x2ced9a0>
irb(main):004:0> s << 'bar'
=> #<StringIO:0x2ced9a0>
irb(main):005:0> s.string
=> "foobar"

Unless I'm missing the reason you're using to_s - that just outputs the object id.

palmsey
+2  A: 

Like other IO-type objects in Ruby, when you write to an IO, the character pointer advances.

>> s = StringIO.new
=> #<StringIO:0x3659d4>
>> s << 'foo'
=> #<StringIO:0x3659d4>
>> s << 'bar'
=> #<StringIO:0x3659d4>
>> s.pos
=> 6
>> s.rewind
=> 0
>> s.read
=> "foobar"

Another way to skin this cat.

Colin Curtin
I don't really *need* this since there's `StringIO#read`, but I'm always a fan of knowing more than one way to do something. +1
James A. Rosen
Ahem, I meant `StringIO#string`
James A. Rosen