From: ts Date: 2002-08-18T19:09:59+09:00 Subject: Re: How do I dup file descriptors in ruby? (diverting STDERR) >>>>> "R" == Richard Ryan writes: R> Yes, those are right along the lines I was thinking, and that actually R> was the first thing I tried but, and maybe I'm being too picky, if I R> put that through irb and check $stderr.fileno I get something other R> than 2. That really kind of bugs me for some reason. It just don't R> seem right. This is because you must use $stderr.reopen, something like this pigeon% cat b.rb #!/usr/bin/ruby def redirect(*args) defout = $defout stdout = $stdout.clone stderr = $stderr.clone $defout = File.new *args $defout.sync = true $stdout.reopen($defout) $stderr.reopen($defout) yield ensure $defout.close $stdout.reopen(stdout) $stderr.reopen(stderr) $defout = defout end redirect('aaa', 'w') do puts "hello world" puts $stdout.fileno puts $stderr.fileno system("ls -l nonexistent_file") system("ls /etc/passwd") end puts "Hello World" puts $stdout.fileno puts $stderr.fileno system("ls -l nonexistent_file") system("ls /etc/group") pigeon% pigeon% b.rb Hello World 1 2 ls: nonexistent_file: No such file or directory /etc/group pigeon% pigeon% cat aaa hello world 1 2 ls: nonexistent_file: No such file or directory /etc/passwd pigeon% Guy Decoux