Skip to content

Instantly share code, notes, and snippets.

@TwP
Created May 1, 2009 15:42
Show Gist options
  • Save TwP/105098 to your computer and use it in GitHub Desktop.
Save TwP/105098 to your computer and use it in GitHub Desktop.
module Kernel
# Settiing this global variable to +false+ will disable rubygems from
# being loaded at all.
$use_rubygems = true
# call-seq:
# require!( string )
# require!( string, gem_version )
# require!( string, gem_name, gem_version )
#
# Attempt to the load the library named _string_ using the standard
# Kernel#require method. If the library cannot be loaded then require
# rubygems and retry the original require of the library.
#
# Raises a LoadError if the library cannot be loaded.
#
# If a _gem_version_ is given, then the rubygems +gem+ command is used to
# load the specific version of the gem. The library _string_ is used for
# the _gem_name_ if one is omitted.
#
def require!( string, *args )
return require(string) if args.empty?
name, version = *args
version, name = name, string if name =~ %r/^[0-9<>=~]/
gem name, version
require(string)
rescue LoadError, NoMethodError
retry if $use_rubygems and require('rubygems')
raise
end
# call-seq:
# require?( string )
# require?( string, gem_version )
# require?( string, gem_name, gem_version )
#
# Attempt to the load the library named _string_ using the standard
# Kernel#require method. If the library cannot be loaded then require
# rubygems and retry the original require of the library.
#
# Returns +true+ if the library was successfully loaded. Returns +false+
# if the library could not be loaded. This method will never raise an
# exception.
#
# If a _gem_version_ is given, then the rubygems +gem+ command is used to
# load the specific version of the gem. The library _string_ is used for
# the _gem_name_ if one is omitted.
#
def require?( string, *args )
require!(string, *args)
return true
rescue LoadError
return false
end
end # module Kernel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment