diff options
author | Xavier Noria <fxn@hashref.com> | 2009-06-28 02:22:57 +0200 |
---|---|---|
committer | Xavier Noria <fxn@hashref.com> | 2009-06-28 02:22:57 +0200 |
commit | b0df8b615e80723e433dd8e3aea98057c321e919 (patch) | |
tree | 711e3380a1790b30383aab1ccede708e00835f6e /railties | |
parent | f73ee375a6d616df97f6d8f36465e2d91757172a (diff) | |
download | rails-b0df8b615e80723e433dd8e3aea98057c321e919.tar.gz rails-b0df8b615e80723e433dd8e3aea98057c321e919.tar.bz2 rails-b0df8b615e80723e433dd8e3aea98057c321e919.zip |
AS guide: added section "Silencing Warnings, Streams, and Exceptions"
Diffstat (limited to 'railties')
-rw-r--r-- | railties/guides/source/active_support_overview.textile | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/railties/guides/source/active_support_overview.textile b/railties/guides/source/active_support_overview.textile index 14618809be..715e5c17d8 100644 --- a/railties/guides/source/active_support_overview.textile +++ b/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+ ... |