Posted By

cczona on 03/13/08


Tagged

ruby


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

webstic


Execute arbitrary code with different level of error messaging


 / Published in: Ruby
 

URL: http://www.caliban.org/ruby/rubyguide.shtml

You can turn off warnings for a section of your code by setting $VERBOSE to nil. Even better is to codify this in a method. Since this method takes a block as its parameter, you can now pass it arbitrary chunks of code to execute without warnings.

  1. def silently(&block)
  2. warn_level = $VERBOSE
  3. $VERBOSE = nil
  4. result = block.call
  5. $VERBOSE = warn_level
  6. result
  7. end
  8.  
  9. # e.g.
  10. silently { require 'net/https' }

Report this snippet  

You need to login to post a comment.