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 \
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.