-
-
Save kogent/133800 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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