Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I select which files to show warnings for?

To show warnings with Ruby one can call ruby with the flag -w. When using libraries Ruby may show lots of warnings so that it is hard to find the warnings from the files you are actually working on. Is it possible to tell ruby to show warnings for some files only?

Here is an example to illustrate the problem. The following loads a library that generates lots of warnings and it also produces a warning on its own by redefining a constant.

require 'grooveshark'

CONSTANT = 'foo'
CONSTANT = 'bar'

The output of ruby -w warning_test.rb is as follows:

/var/lib/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/exceptions.rb:157: warning: assigned but unused variable - message
/var/lib/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/exceptions.rb:167: warning: assigned but unused variable - message
/var/lib/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/response.rb:11: warning: method redefined; discarding old body
/var/lib/gems/1.9.1/gems/rest-client-1.6.7/lib/restclient/payload.rb:47: warning: mismatched indentations at 'end' with 'case' at 40
/var/lib/gems/1.9.1/gems/macaddr-1.6.1/lib/macaddr.rb:63: warning: assigned but unused variable - status
/var/lib/gems/1.9.1/gems/macaddr-1.6.1/lib/macaddr.rb:63: warning: assigned but unused variable - stderr
/var/lib/gems/1.9.1/gems/macaddr-1.6.1/lib/macaddr.rb:56: warning: assigned but unused variable - re
/var/lib/gems/1.9.1/gems/macaddr-1.6.1/lib/macaddr.rb:59: warning: assigned but unused variable - null
/var/lib/gems/1.9.1/gems/systemu-2.5.2/lib/systemu.rb:126: warning: shadowing outer local variable - cid
/var/lib/gems/1.9.1/gems/systemu-2.5.2/lib/systemu.rb:213: warning: shadowing outer local variable - buf
/var/lib/gems/1.9.1/gems/systemu-2.5.2/lib/systemu.rb:215: warning: shadowing outer local variable - buf
/var/lib/gems/1.9.1/gems/systemu-2.5.2/lib/systemu.rb:208: warning: assigned but unused variable - ignored
/var/lib/gems/1.9.1/gems/systemu-2.5.2/lib/systemu.rb:252: warning: shadowing outer local variable - key
/var/lib/gems/1.9.1/gems/systemu-2.5.2/lib/systemu.rb:249: warning: assigned but unused variable - ignored
/var/lib/gems/1.9.1/gems/grooveshark-0.2.7/lib/grooveshark/user.rb:65: warning: method redefined; discarding old playlists
/var/lib/gems/1.9.1/gems/grooveshark-0.2.7/lib/grooveshark/user.rb:93: warning: method redefined; discarding old favorites
warning_test.rb:4: warning: already initialized constant CONSTANT

In this case I would only like to see only the last warning

warning_test.rb:4: warning: already initialized constant CONSTANT

Since the other warnings are due to libraries I do not have direct control over the warnings for them are not as interesting. I would like to be able to tell Ruby to only show warnings for the project one is working.

I realize that defining the current might be tricky. Maybe to only show warnings for the given file and files included via require_relative would do. Another alternative is to show warnings for files that are close in the directory structure to the given file.

like image 874
N.N. Avatar asked Nov 13 '22 11:11

N.N.


1 Answers

You could redirect stderr to stdout and then grep. Here's an example:

ruby -w warning_test.rb 2>&1 | grep "warning_test"

like image 117
Michael Frederick Avatar answered Nov 15 '22 05:11

Michael Frederick