Permalink
Browse files

AS guide: added section "Silencing Warnings, Streams, and Exceptions"

  • Loading branch information...
1 parent f73ee37 commit b0df8b615e80723e433dd8e3aea98057c321e919 @fxn fxn committed Jun 28, 2009
Showing with 25 additions and 0 deletions.
  1. +25 −0 railties/guides/source/active_support_overview.textile
View
25 railties/guides/source/active_support_overview.textile
@@ -185,6 +185,31 @@ The method +Hash#to_query+ accepts an optional namespace for the keys:
# => "user%5Bid%5D=89&user%5Bname%5D=John+Smith"
</ruby>
+h4. Silencing Warnings, Streams, and Exceptions
+
+The methods +silence_warnings+ and +enable_warnings+ change the value of +$VERBOSE+ accordingly for the duration of their block, and reset it afterwards:
+
+<ruby>
+silence_warnings { Object.const_set "RAILS_DEFAULT_LOGGER", logger }
+</ruby>
+
+You can silence any stream while a block runs with +silence_stream+:
+
+<ruby>
+silence_stream(STDOUT) do
+ # STDOUT is silent here
+end
+</ruby>
+
+Silencing exceptions is also possible with +suppress+. This method receives an arbitrary number of exception classes. If an exception is raised during the execution of the block and is +kind_of?+ any of the arguments, +suppress+ captures it and returns silently. Otherwise the exception is reraised:
+
+<ruby>
+# If the user is locked the increment is lost, no big deal.
+suppress(ActiveRecord::StaleObjectError) do
+ current_user.increment! :visits
+end
+</ruby>
+
h3. Extensions to +Module+
...

0 comments on commit b0df8b6

Please sign in to comment.