Switching off warnings temporarily

S

Stephan Kämper

Hi,

although always running code with '-w' is a good idea, you sometimes
might like to switch off warnings locally, temporarily or both.

Now, http://www.caliban.org/ruby/rubyguide.shtml#warnings shows how to
accomplish this (I just used 'yield' instead of 'call'ing a passed block).

(I know that assigning the result of puts to whatever *is* useless, its
just to produce a warning)

---- Code starts here ----
def silently
warn_level = $VERBOSE
$VERBOSE = false
res = yield
$VERBOSE = warn_level
res
end

bark = puts ( "Here be Dragons!" )

keep_silent = silently do
any_line_of_code_will_do = 1
puts ( "Creatures under a smooth surface" )
nil
end

bark = puts ( "Here be dragons!" )

---- End of the Code ----

Remark: That really reads great: 'silently do ....' :)
(We might like to see just the opposite 'verbosely do' with warnings
switch on, no matter what.)

Now I came up with four ways to run this code: With or with out the '-w'
command line option and with or without the (apparently useless) line
commented out.

My expectation was ('is' actually) to get two warnings each way.
However

Code run with | No. of warnings
--------------------------------
#any... -w | 3
any... -w | 3
#any... | 3
any... | 2

To get the result I expect, I apparently need to run the code without
the '-w' option and put some (useless) code before the line that might
create a warning.

I have to admit that I just don't get it...
Why is that? Do you have any explanations?

Happy rubying

Stephan
 
S

Stephan Kämper

Stephan said:
---- Code starts here ----
def silently
warn_level = $VERBOSE
$VERBOSE = false
res = yield
$VERBOSE = warn_level
res
end

bark = puts ( "Here be Dragons!" )

keep_silent = silently do
any_line_of_code_will_do = 1
puts ( "Creatures under a smooth surface" )
nil
end

bark = puts ( "Here be dragons!" )

---- End of the Code ----

To get the result I expect, I apparently need to run the code without
the '-w' option and put some (useless) code before the line that might
create a warning.

Why is that? Do you have any explanations?

This happens on both Linux and WinXP, BTW. It's a recent Ruby snapshot
on the Linux box and the most recent One-Click-Windows-Installer (1CWI)
on WinXP.

Happy rubying

Stephan
 
C

Carlos

---- Code starts here ----
def silently
warn_level = $VERBOSE
$VERBOSE = false
res = yield
$VERBOSE = warn_level
res
end

bark = puts ( "Here be Dragons!" )

keep_silent = silently do
any_line_of_code_will_do = 1
puts ( "Creatures under a smooth surface" )
nil
end

bark = puts ( "Here be dragons!" )

---- End of the Code ----

Remark: That really reads great: 'silently do ....' :)
(We might like to see just the opposite 'verbosely do' with warnings
switch on, no matter what.)

Now I came up with four ways to run this code: With or with out the '-w'
command line option and with or without the (apparently useless) line
commented out.

My expectation was ('is' actually) to get two warnings each way.
However

Code run with | No. of warnings
--------------------------------
#any... -w | 3
any... -w | 3
#any... | 3
any... | 2

To get the result I expect, I apparently need to run the code without
the '-w' option and put some (useless) code before the line that might
create a warning.

I have to admit that I just don't get it...
Why is that? Do you have any explanations?

I can't explain the "any_line_will_do" thing, but the warnings you are
getting ("don't puts spaces before arg parens") are compile-time (ie, when
your code is read). With $VERBOSE you can disable run-time warnings only
(because the variable is set at run time...).

* Compile time:
$ ruby -we 'puts ("Hi")'
-e:1: warning: (...) interpreted as grouped expression
Hi

$ ruby -we '$VERBOSE=nil; puts ("Hi")'
-e:1: warning: (...) interpreted as grouped expression
Hi

* Run time:
$ ruby -we 'A=1; A=2'
-e:1: warning: already initialized constant A

$ ruby -we '$VERBOSE=nil; A=1; A=2'

(no warning)
--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,120
Latest member
ShelaWalli
Top