Skip to content

Instantly share code, notes, and snippets.

@TwP
Created March 30, 2009 20:26
Show Gist options
  • Save TwP/87873 to your computer and use it in GitHub Desktop.
Save TwP/87873 to your computer and use it in GitHub Desktop.
# This is a useful little pattern for working with gems without
# explicitly requiring rubygems in your code.
#
# The approach taken is to require a library and catch any LoadError
# that might get thrown. In the rescue clause we require rubygems.
#
# The require method is nice in that it will return true if the code
# was loaded, and it returns false if the code was _already_ loaded.
# If the require of rubygems returns true, we will retry our original
# require. It will either succeed this time (since we now have rubygems
# loaded) or it will raise another LoadError. Now we raise the error
# again since rubygems was already required.
begin
require 'logging'
rescue LoadError
retry if require 'rubygems'
raise
end
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment