From: ahoward Date: 2003-05-07T08:13:17+09:00 Subject: Re: capture output On Wed, 7 May 2003, Simon Strandgaard wrote: $stdout and friends are PROCESS based, Kernel.system starts a new process with it's OWN $stdout and friends - they are not inherited. you could do something like this: ~/eg/ruby > cat main.rb require 'stringio' def capture raise unless block_given? a, b, c = $defout, $stderr, $stdout buf = StringIO.new(s = "", "w") begin #$defout, $stderr, $stdout = buf $defout, $stderr, $stdout = buf, buf, buf yield ensure buf.close $defout, $stderr, $stdout = a, b, c end s.split(/\n/) end res = capture { puts "1" #system("ruby output.rb") # BOOM - Output is not being captured? load 'output.rb' puts "2" } puts res.collect{|s| "OK "+s }.join("\n") ~/eg/ruby > cat output.rb $stderr.puts "stderr" $stdout.puts "stdout" $defout.puts "defout" ~/eg/ruby > ruby main.rb OK 1 OK stderr OK stdout OK defout OK 2 -a -- ==================================== | Ara Howard | NOAA Forecast Systems Laboratory | Information and Technology Services | Data Systems Group | R/FST 325 Broadway | Boulder, CO 80305-3328 | Email: ara.t.howard@fsl.noaa.gov | Phone: 303-497-7238 | Fax: 303-497-7259 ====================================