當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Ruby Open3.popen2e用法及代碼示例


本文簡要介紹ruby語言中 Open3.popen2e 的用法。

用法

popen2e(*cmd, &block)

Open3.popen2e Open3.popen3 相似,隻是它合並了標準輸出流和標準錯誤流。

方塊形式:

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

非阻塞形式:

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

有關可選散列參數 envopts 的信息,請參見 Process.spawn

例子:

# check gcc warnings
source = "foo.c"
Open3.popen2e("gcc", "-Wall", source) {|i,oe,t|
  oe.each {|line|
    if /warning/ =~ line
      ...
    end
  }
}

相關用法


注:本文由純淨天空篩選整理自ruby-lang.org大神的英文原創作品 Open3.popen2e。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。