From within a Ruby script, verbosity can be tested with the value of the $VERBOSE global variable, which can have three states:
nil in case verbosity level was “0” (silence)
false for level “1” (medium); this is the default
true for level “2” (verbose); this is verbose mode.
I started to play with my code to understand the difference between them:
@ubuntu:~$ ruby -W0 -e 'a=10;a=10;@foo'
@ubuntu:~$ ruby -W1 -e 'a=10;a=10;@foo'
@ubuntu:~$ ruby -W2 -e 'a=10;a=10;@foo'
-e:1: warning: possibly useless use of a variable in void context
-e:1: warning: instance variable @foo not initialized
@ubuntu:~$
But really could not understand what the difference between W1 and W0. Could anyone help me to understand the difference?
To see the real difference, you have to print some text on the $STDERR. I’ve made the following change on your example:
Note that running the code with the
W0flag, nothing will appear in the terminal when you execute. Now, if you run with theW1, you will see the “error” message generated by theKernel#warn:And finally, the
W2will show the error and the warnings generated by the interpreter.