Skip to content

Instantly share code, notes, and snippets.

Created September 30, 2021 17:03
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save eregon/797eb71fd82c8abe47da0f0d97ec89ca to your computer and use it in GitHub Desktop.
command = ARGV
start = Process.clock_gettime Process::CLOCK_MONOTONIC, :millisecond
IO.popen(command, err: [:child, :out]) do |io|
while line = io.gets
now = Process.clock_gettime Process::CLOCK_MONOTONIC, :millisecond
puts "[#{"%5d" % (now - start)}] #{line}"
Copy link

eregon commented Sep 30, 2021


$ ruby time_output.rb ruby -e 'STDOUT.sync = true; sleep 0.1; STDOUT.puts "out"; sleep 0.1; STDERR.puts "err"'
[  134] out
[  234] err

Note the needed STDOUT.sync = true (or STDOUT.flush), otherwise STDOUT might be block-buffered since it's not a TTY (but a pipe).
This is currently not needed on TruffleRuby as there is no write buffering (only read buffering).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment