Quantcast
Channel: Active questions tagged ruby - Stack Overflow
Viewing all articles
Browse latest Browse all 4612

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

$
0
0

I'm working with an IO object (some STDOUT output text), and I'm trying to convert it to a string so that I can do some text processing. I would like to do something like this:

my_io_object = $stdout#=> #<IO:<STDOUT>>my_io_object.puts('hi')  #note: I know how to make 'hi' into a string, but this is a simplified example#=>himy_io_object.to_s

I have tried a few things and gotten a few errors:

my_io_object.read #=> IOError: not opened for readingmy_io_object.open#=> NoMethodError: private method `open' called for #<IO:<STDOUT>>IO.read(my_io_object)#=> TypeError: can't convert IO into String

I've read through the IO class methods, and I can't figure out how to manipulate the data in that object. Any suggestions?


Viewing all articles
Browse latest Browse all 4612

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>