From: gotoken@... (GOTO Kentaro) Date: 2000-08-17T16:03:17+09:00 Subject: [ruby-list:24253] Re: コマンドの STDERR への出力を補足したい。 ごとけんです In message "[ruby-list:24250] コマンドの STDERR への出力を補足したい。" on 00/08/17, Kengo Nakajima writes: >Rubyのコマンド実行の文において、 > > a = `gcc no_such_file.c` > >という感じで標準出力を捕捉することができますが、標準エラー出力に >関しては、どうやったら捕捉できるでしょうか?できれば一時ファイルを >使いたくないのですが。 ブロックされる場合も考慮するなら `command` ほど簡便な方法は ありませんが open3 を用いるのがよいでしょう。 もう解決されたようですが、以下の例では、実行の終了を待ちコマ ンドの出力とエラーを配列にして返します。 # これって open3 にあってもよいような。 require "open3" require "thread" module Open3 def out_and_err(cmd) _in, _out, _err = popen3(cmd) q = Queue.new # queue to wait the execution [_out, _err].each do |io| Thread.start do # make thread to avoid blocking q.enq [io, io.to_a] # enqueue when io is closed end end result = {} 2.times do from, array = q.deq result[from] = array end [result[_out], result[_err]] end module_function :out_and_err end out, err = Open3::out_and_err("gcc no_such_file1.c no_such_file2.c") p out, err