Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amarshall/906427 to your computer and use it in GitHub Desktop.
Save amarshall/906427 to your computer and use it in GitHub Desktop.
# Loads the specified gem from RVM's current @global gemset if using
# Bundler and not included in the current Gemfile. Falls back
# accordingly if not using Bundler or RVM.
#
# @note Doesn't load gems not loaded by Bundler if using Bundler and
# not using RVM, or if gem is located somewhere other than the
# @global gemset.
# @param [String] The gem to require
# @raise [LoadError] If the gem couldn't be loaded
# Adapted from <https://gist.github.com/794915>
def require_global_gem_without_bundler(gem)
if defined? ::Bundler
# Check if the gem has already been loaded by Bundler
unless Bundler.require.map { |i| i.name }.include? gem
if global_gem_dir = $LOAD_PATH.grep(/@global.*?lib/).first
global_gem_dir = global_gem_dir.gsub %r{@global/gems/.*$}, '@global/gems'
Dir["#{global_gem_dir}/*"].to_a.each do |gem_path|
if File.basename(gem_path).gsub(/-(\d\.?)+$/, '') == gem
$LOAD_PATH << "#{gem_path}/lib"
require gem
return
end
end
end
# Either RVM's @global gemset isn't available, or the gem isn't
# installed in it, either way, gem wasn't be loaded.
raise LoadError
end
else
# Bundler isn't being used, load gem normally
require gem
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment