如果你是一个支持它的平台上,你可以用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'一样,你想避免什么?如果他们*没有被父母程序捕获和管理,你希望他们去哪里? –