Created
September 12, 2013 05:38
-
-
Save proglottis/6533389 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
def fork_with_output(&block) | |
std_pipe = IO.pipe | |
err_pipe = IO.pipe | |
fork do | |
$stdout.reopen(std_pipe[1]) | |
$stderr.reopen(err_pipe[1]) | |
block.call | |
std_pipe.each(&:close) | |
err_pipe.each(&:close) | |
exit! | |
end | |
std_pipe[1].close | |
err_pipe[1].close | |
[std_pipe[0], err_pipe[0]] | |
end | |
std, err = fork_with_output do | |
puts "start child" | |
$stderr.puts "EROOROOROOORORR" | |
sleep 1 | |
puts "stop child" | |
end | |
$stdout.write std.read | |
$stderr.write err.read |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment