tags:

views:

144

answers:

1

In Java, one can create an IO stream from a String like so:

Reader r = new StringReader("my text");

I'd like to be able to do the same in Ruby so I can take a string and treat it as an IO stream.

+4  A: 
r = StringIO.new("my text")

And here's the documentation.

molf
One key thing that bit me (it's in the documentation; I just missed it) is that to get the result out you use #string, not #to_s. #to_s just tells you it's a StringIO. #string gives you the built result.
James A. Rosen
See also http://stackoverflow.com/questions/10323/why-doesnt-ruby-have-a-real-stringbuffer-or-stringio
James A. Rosen