Inconsistency in shared state between parent/child processes

i am very confused about how/when global state is shared between
parent-child processes in ruby, look at the following :

#!/usr/bin/env ruby

require ‘delegate’

$errlog0 = open ‘errlog0’, ‘w’
$errlog1 = open ‘errlog1’, ‘w’

···

#############################################

TEST ZERO

#############################################
class DupIO < SimpleDelegator
def initialize io
super io
end
end

$stderr = DupIO.new $errlog0

puts “RUNNING TEST ZERO”

$stderr.puts ‘this is redirected into errlog’

this is NOT!!!

ls some/command/which/produces/stderr

#############################################

TEST ONE

#############################################
$stderr = $errlog1

puts “RUNNING TEST ONE”

$stderr.puts ‘this is redirected into errlog’

this is TOO!!!

ls some/command/which/produces/stderr


in TEST ZERO it seems the global variable $stderr has NO impact on a
spawned subprocess, yet in TEST ONE is certainly does?? can anyone
shed light on this?

-ara howard
ahoward@fsl.noaa.gov

i am very confused about how/when global state is shared between
parent-child processes in ruby, look at the following :

$stderr = DupIO.new $errlog0

this is NOT!!!

ls some/command/which/produces/stderr

$stderr = $errlog1

this is TOO!!!

ls some/command/which/produces/stderr

Because when you assign $stderr or $stdout to a IO object, Ruby does some
magic and dups the file descriptor. But when you assign it to some other
object, it only changes the binding of the variable; and everything that
goes to stderr or stdout by other meanings than calling methods… well,
goes to stderr or stdout.