Are there alternatives to rescue LoadError for Ruby?

Viewed 1058

I read the blog post JRuby Performance: Exceptions are not flow control which advocated avoiding using Exceptions except for exceptional circumstances.

I realized that I'm guilty of using rescue to handle LoadErrors on a regular basis.

Is there an alternative to require that tries to load a file if it exists, but doesn't throw an exception if it doesn't?

Background: If you're wondering "why have you got requires that you don't absolutely require?", here's my story:

  1. While I was programming for Ruby 1.8, I used require "rdoc/usage" so that I could give usage information if I didn't enter the correct number of parameters in my command-line application. This throws an exception on out-of-the-box 1.9.
  2. Part of my application involves code to manipulate win32ole when it's running on my Windows desktop. This causes a LoadError if the files involved were run under the Linux server that does the heavy computational work. The files that use win32ole also has other code that is tested in my test suite, so when running my test suite under Linux, I have to require those files. I ought to split up such files, but that seems a bit like yak shaving.
2 Answers
Related