pipe = IO.popen("-","w+") if pipe pipe.puts "Get a job!" $stderr.puts "Child says '#{pipe.gets.chomp}'" else $stderr.puts "Dad says '#{gets.chomp}'" puts "OK" end
输出: Dad says 'Get a job!' Child says 'OK'
子进程和父进程的输出互换了,好心人帮忙解释下产生这种情况具体的实现过程是怎样的啊
If a block is given with a cmd of “-”, the block will be run in two separate processes: once in the parent, and once in a child. The parent process will be passed the pipe object as a parameter to the block, the child version of the block will be passed nil, and the child’s standard in and standard out will be connected to the parent through the pipe. Not available on all platforms.
#1 楼 @ChanceDoor 没有写反啊
回 LZ,就是子进程的 STDIN 和 STDOUT 连接到了父进程的 pipe 上。
#2 楼 @kenshin54 the child version of the block will be passed nil
那他的 if pipe 不是应该是父进程么?