Execute arbitrary code with different level of error messaging


/ Published in: Ruby
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  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' }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.