For most practical purposes, can you put something in $stdout that responds to write , flush , sync , sync= and tty? .
In this example, I am using a modified Queue from stdlib.
class Captor < Queue alias_method :write, :push def method_missing(meth, *args) false end def respond_to_missing?(*args) true end end stream = Captor.new orig_stdout = $stdout $stdout = stream puts_thread = Thread.new do loop do puts Time.now sleep 0.5 end end 5.times do STDOUT.print ">> #{stream.shift}" end puts_thread.kill $stdout = orig_stdout
You need something like this if you want to actively act on the data, and not just look at it after completing the task. Using StringIO or a file will be problematic with multiple threads trying to synchronize read and write at the same time.
Kimmo Lehto Jun 09 '17 at 12:07 on 2017-06-09 12:07
source share