“...I've been working since 2008 with Ruby / Ruby on Rails, love a bit of Elixir / Phoenix and learning Rust. I also poke through other people's code and make PRs for OpenSource Ruby projects that sometimes make it. Currently working for InPay who are based in Denmark...”

Rob Lacey
Senior Software Engineer, Copenhagen, Denmark

Redirecting STDERR

So you don’t like STDERR vomming all over your console?

This script almost works but doesn’t redirect anything that is already hold the original file descriptor, e.g. C extensions.

#!/usr/bin/env ruby

STDERR.puts "Something"
$stderr = File.open(File::NULL, 'w')
STDERR.puts "...and something else"

This however reopens the same file descriptor and covers all bases.

#!/usr/bin/env ruby

STDERR.puts "Something"
$stderr.reopen('/dev/null', 'w')
$stderr.sync = true
STDERR.puts "...and nothing"

And we get…

./test.rb 
Something