Why doesn't Ruby have a real StringBuffer or StringIO?

29,542

Solution 1

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

Thus, change your code to:

s = StringIO.new
s << 'foo'
s << 'bar'
s.string

Solution 2

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"

Solution 3

I did some benchmarks and the fastest approach is using the String#<< method. Using StringIO is a little bit slower.

s = ""; Benchmark.measure{5000000.times{s << "some string"}}
=>   3.620000   0.100000   3.720000 (  3.970463)

>> s = StringIO.new; Benchmark.measure{5000000.times{s << "some string"}}
=>   4.730000   0.120000   4.850000 (  5.329215)

Concatenating strings using the String#+ method is the slowest approach by many orders of magnitude:

s = ""; Benchmark.measure{10000.times{s = s + "some string"}}
=>   0.700000   0.560000   1.260000 (  1.420272)

s = ""; Benchmark.measure{10000.times{s << "some string"}}
=>   0.000000   0.000000   0.000000 (  0.005639)

So I think the right answer is that the equivalent to Java's StringBuffer is simply using String#<< in Ruby.

Solution 4

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.

Solution 5

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
Share:
29,542
James A. Rosen
Author by

James A. Rosen

Updated on July 09, 2022

Comments

  • James A. Rosen
    James A. Rosen almost 2 years

    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?

    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, is really bad.

    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.

    • Dan Rosenstark
      Dan Rosenstark about 15 years
      "Mega Maid?" Never heard of her. Never really believed in StringBuffers either, but I always used them for fear of someone seeing my code. But really, does that stuff ever add up?
    • Stephen Eilert
      Stephen Eilert over 13 years
      Probably a 'SpaceBalls' reference.
    • oligan
      oligan almost 13 years
      Mega maid has been deleted as collateral damage from getting rid of profanity.
    • Theo
      Theo almost 13 years
      Your string joining example is not equivalent to the Java code. As you mention, Ruby strings are mutable, so in Ruby you just do: stuff.inject('') { |res, s| res << s.to_s }. You can safely rely on Ruby strings being mutable, it's not going to change as it would break every single Ruby application in existence.
    • hcarreras
      hcarreras almost 10 years
      I really don't understand why StringIO doesn't have a to_s method. It's a class that manage a string, so if you want that string you have to specifically ask for it. It should have a to_s method since is the ruby convention, but it doesn't. (Someone can correct me if I'm wrong)
    • Franklin Yu
      Franklin Yu over 7 years
      @Theo In Ruby 3, string literals would be immutable. However we can still use mutable String.new or +''.
  • James A. Rosen
    James A. Rosen almost 15 years
    That's certainly true, and it's great for short interpolations. It's lousy for building long strings like HTML pages, though. See en.wikipedia.org/wiki/Schlemiel_the_painter%27s_Algorithm
  • James A. Rosen
    James A. Rosen over 14 years
    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
    James A. Rosen over 14 years
    Ahem, I meant StringIO#string
  • Earl Jenkins
    Earl Jenkins over 12 years
    Sure, but for building HTML pages, why not use something that's build for that function, like HAML or ERB?
  • deltav
    deltav over 10 years
    re: "Schlemiel the Painter's algorithm" If you are using StringIO as in the above, Joel Spolsky's criticism does not apply. StringIO has a seek pointer just like a file. There is no need to recompute the end of the string each time. And for longer strings you can use %{ } or a library like the ones Earl has suggested.
  • Jared Beck
    Jared Beck about 10 years
    What version of ruby was used for this benchmark, please?
  • Nikkolasg
    Nikkolasg over 9 years
    Wow. So this should be the right answer as String is fastest. Tested on ruby 2.1.5 and same results.
  • akostadinov
    akostadinov over 7 years
    and what happens when ruby makes strings immutable? Such micro-optimizations are a hell at the end.
  • nothing-special-here
    nothing-special-here about 7 years
    What happens if you want to add some characters to very long string? I think StringIO then will be faster
  • KARASZI István
    KARASZI István about 4 years
    The string concatenation solution won't work with frozen string literals, but StringIO works.
  • psmith
    psmith about 2 years
    Interesting to see how much faster ruby and computers together have become in the past years. On ruby 2.7.5 i got these results in order: 0.856717 (4.6x faster), 1.002285 (5.3x), 0.553703 (2.5x), 0.001409 (3.9x). I'm on a MBP 2017.