I’ve been doing a lot of pontification lately. Time for some code.
Warnings are a hot topic in the Ruby community. Some folks love ‘em. Some folks hate ‘em. What we can all agree on is that they do us no good when loading third-party gems in strict mode overruns our output with warnings we have no control over.
Here’s a file that will generate several warnings under -w:
Here’s the good news: Ruby uses the mutable $stderr global for all of its warnings. Why is this good news? Behold:
Let’s give it a try:
When I run this under Ruby 1.9.2, here’s what I get:
Loading messy.rb ---- Begin ---- /home/avdi/Dropbox/scratch/warnings/messy.rb:2: warning: useless use of a variable in void context /home/avdi/Dropbox/scratch/warnings/messy.rb:4: warning: method redefined; discarding old bar /home/avdi/Dropbox/scratch/warnings/messy.rb:3: warning: previous definition of bar was here /home/avdi/Dropbox/scratch/warnings/messy.rb:6: warning: already initialized constant BAZ ---- End ---- Loading messy.rb with warnings silenced ---- Begin ---- ---- End ----
So there you have it: a way to load warning-ridden files in -w mode without flooding your output.
I can think of lots of improvements–for starters, there’s no reason to fill up a StringIO buffer with output we’re just going to ignore. But I need to get ready for my trip to Ruby Hoedown, so I’ll leave the tinkering to you.
EDIT: Before you get too excited, though, the code above (or code using VERBOSE=nil, as commenters have pointed out) only solves a small part of the problem. If third-party code generates warnings at runtime, you’ll have to wrap every single call to the third-party code with a warnings silencer block.