Skip to content

Instantly share code, notes, and snippets.

@kogent
Forked from TwP/play-nice.rb
Created June 22, 2009 04:22
Show Gist options
  • Save kogent/133800 to your computer and use it in GitHub Desktop.
Save kogent/133800 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