当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Ruby Open3.popen2用法及代码示例


本文简要介绍ruby语言中 Open3.popen2 的用法。

用法

popen2(*cmd, &block)

Open3.popen2 Open3.popen3 类似,只是它不为标准错误流创建管道。

方块形式:

Open3.popen2([env,] cmd... [, opts]) {|stdin, stdout, wait_thr|
  pid = wait_thr.pid # pid of the started process.
  ...
  exit_status = wait_thr.value # Process::Status object returned.
}

非阻塞形式:

stdin, stdout, wait_thr = Open3.popen2([env,] cmd... [, opts])
...
stdin.close  # stdin and stdout should be closed explicitly in this form.
stdout.close

有关可选散列参数 envopts 的信息,请参见 Process.spawn

例子:

Open3.popen2("wc -c") {|i,o,t|
  i.print "answer to life the universe and everything"
  i.close
  p o.gets #=> "42\n"
}

Open3.popen2("bc -q") {|i,o,t|
  i.puts "obase=13"
  i.puts "6 * 9"
  p o.gets #=> "42\n"
}

Open3.popen2("dc") {|i,o,t|
  i.print "42P"
  i.close
  p o.read #=> "*"
}

相关用法


注:本文由纯净天空筛选整理自ruby-lang.org大神的英文原创作品 Open3.popen2。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。