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
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