Warning¶
Sapphire uses structured warnings. This allows you to enable or disable warnings either globally or within a block. It also makes warnings testable.
Synopsis¶
You "raise" a warning the same way you raise an exception. The difference is that warnings don't break control flow, and aren't rescuable. They are informational only.
class Foo
def old_method
warn DeprecatedWarning, "this method is old"
# some old code...
end
end
A warn call without a specific class raises a StandardWarning
class Foo
def old_method
warn "this method is old" # No warning class defaults to StandardWarning
end
end
Disabling Warnings¶
A Warning class can be disabled locally or globally. If a block is provided, the warning is disabled only within the scope of that block. Otherwise, it is disabled at the point of the call.
# Disable globally DeprecatedWarning.disable # Disable within code block DeprecatedWarning.disable do # Ignore deprecation warnings within this block end
Advantages¶
1. Better control. No more "on" vs "off".
2. Better information. You can get backtrace information.
3. Testable. Both that a warning is raised, and the warning message.
Article¶
http://www.oreillynet.com/ruby/blog/2008/02/structured_warnings_now.html