Forum: Ruby Immediately output stdout

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Eebd66b9bbf5462ed5d13a9aa7987647?d=identicon&s=25 Nick K. (hitryy)
on 2016-10-07 17:11
0
down vote
favorite


How can I immediately output stdout? stdout is going to print after all
input is complete.

require 'open3'
def run(cmd)
    Open3.popen3(cmd) do |stdin, stdout, stderr, thread|

    Thread.new do
      stdout.each {|l| puts l}
    end

    Thread.new do
      while thread.alive?
        stdin.puts $stdin.gets
      end
    end

    thread.join
  end
end

run ("ruby file_to_test.rb")

file_to_test.rb:

puts "please, enter s"
puts "please, enter q"

s = gets.chomp!
q = gets.chomp!

puts s
puts q

The result after running main.rb is:

somestring
somestring2
please, enter s
please, enter q
somestring
somestring2

How can I immediately output stdout?
3a81ced4012650ca2004c9bff7b18202?d=identicon&s=25 Ronald F. (rovf)
on 2016-11-07 14:39
Set stdout to unbuffered. In your case:

stdout.sync = true
This topic is locked and can not be replied to.