如果你是一個支持它的平臺上,你可以用pipe
,fork
和exec
做到這一點:
# create a pipe
read_io, write_io = IO.pipe
child = fork do
# in child
# close the write end of the pipe
write_io.close
# change our stdin to be the read end of the pipe
STDIN.reopen(read_io)
# exec the desired command which will keep the stdin just set
exec 'the_child_process_command'
end
# in parent
# close read end of pipe
read_io.close
# write what we want to the pipe, it will be sent to childs stdin
write_io.write "this will go to child processes stdin"
write_io.close
Process.wait child
你爲什麼不花時間來顯示你正在嘗試一些例子做。不要讓我們猜測或試圖設想你的代碼。這是浪費時間。請參閱http://sscce.org/ –
@theTinMan我不明白在這種情況下如何適用。我沒有一些代碼被破壞,我正在嘗試修復,我有一個關於Ruby API的特定問題需要回答。 我已經列出了三個不起作用的例子。你想讓我把這些例子放在上下文中並證明它們不起作用嗎? :confused: – Ajedi32
捕獲STDOUT和STDERR與'open3'一樣,你想避免什麼?如果他們*沒有被父母程序捕獲和管理,你希望他們去哪裏? –