$stderr is Ruby’s reassignable global variable for the current standard error stream. Unlike the STDERR constant, it can be assigned a different IO object, allowing a program to redirect error output without losing the original stream. section 12.1.3 section 12.1.1
Redirecting errors to a file
record = File.open("/tmp/record", "w") $stdout = record $stderr = $stdout puts "This is a record" z = 10/0 Both ordinary output and the resulting division-by-zero message are written to the file because each global variable points to the same handle. section 12.1.3
Listing 12.1 Directing all output to a single file
record = File.open("/tmp/record", "w")
old_stdout = $stdout
$stdout = record
$stderr = $stdout
puts "This is a record"
z = 10/0
Shows $stderr being assigned to the same file handle as $stdout before ordinary and failing operations run. section 12.1.3
