Capturing output to a File works fine.. But not to StringIO, Why ???



using File works as a charm:

> cat short3.rb
$stdout = File.open("log.txt","w")

$defout = $stderr = $stdout
$stdout.sync = true
$stderr.sync = true
$defout.sync = true
puts "1"
system("ruby output.rb")
puts "2"
puts `ruby output.rb`
puts "3"
> cat output.rb
$stderr.puts "stderr"
$stdout.puts "stdout"
$defout.puts "defout"
> ruby short3.rb
> cat log.txt
1
stderr
stdout
defout
2
stderr
stdout
defout
3
>


using StringIO causes trouble!  
The content of "log.txt" is different... Why ???

> cat short2.rb
require 'stringio'
s = "test: "
$stdout = StringIO.open(s,"w")

$defout = $stderr = $stdout
$stdout.sync = true
$stderr.sync = true
$defout.sync = true
puts "1"
system("ruby output.rb")
puts "2"
puts `ruby output.rb`
puts "3"

# output result to file
f = File.open("log.txt", "w")
f.puts s
f.close
> cat output.rb
$stderr.puts "stderr"
$stdout.puts "stdout"
$defout.puts "defout"
> ruby short2.rb
stderr
stdout
defout
stderr
> cat log.txt
1
2
stdout
defout
3
>


What should I do in order to capture output to a string ???

--
Simon Strandgaard