Skip to content

Instantly share code, notes, and snippets.

@noprompt
Last active March 28, 2016 13:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save noprompt/e365348c60ad19f39102 to your computer and use it in GitHub Desktop.
Save noprompt/e365348c60ad19f39102 to your computer and use it in GitHub Desktop.
# @return [Array<String>]
def gem_paths
Gem.path.flat_map { |path| Dir.glob("#{path}/gems/*") }
end
# @param [String] gem_name
# @return [Boolean]
# @raise [LoadError]
def require_gem(gem_name)
if defined?(::Bundler)
gem_pattern = Regexp.new(gem_name)
matching_paths = gem_paths.grep(gem_pattern)
if matching_paths.any?
matching_paths.each do |gem_path|
$LOAD_PATH.push "#{gem_path}/lib"
end
begin
require(gem_name)
rescue LoadError => error
# Attempt to load any transitive dependencies.
require_gem(error.path)
require_gem(gem_name)
end
else
fail LoadError, "Gem #{gem_name} not found."
end
else
require(gem_name)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment