How can I convert an IO object to a string in Ruby? How can I convert an IO object to a string in Ruby? ruby ruby

How can I convert an IO object to a string in Ruby?


I solved this by directing my output to a StringIO object instead of STDOUT:

> output = StringIO.new#<StringIO:0x007fcb28629030>> output.puts('hi')nil> output.string"hi\n"


STDOUT accepts strings, it does not provide strings. You can write to it, but cannot read from it.

STDOUT.write("hello") # => helloSTDOUT.read # => IOError: not opened for reading