Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically determine gem's path using bundler

Tags:

ruby

bundler

I know you can do

bundle show gem_name

to show the path of some gem.

How do you do that from within the code using the Bundler object?

like image 700
gylaz Avatar asked Feb 17 '12 02:02

gylaz


People also ask

How do I know if gem is installed?

Since your goal is to verify a gem is installed with the correct version, use gem list . You can limit to the specific gem by using gem list data_mapper . To verify that it's installed and working, you'll have to try to require the gem and then use it in your code.

Where are gems installed bundler?

I know that when using gem install , the gem will be stored under /home/username/. rvm/gems/, under which gemset the gem was installed.

How does bundler work in Rails?

Think of bundler as a package management tool. So bundle install command will install all gems to the system that are listed in Gemfile as well as their dependencies. If the gem was not previously installed it will grab it from the gemcutter repo.


1 Answers

Have a look at how they do it in cli.rb

def locate_gem(name)
  spec = Bundler.load.specs.find{|s| s.name == name }
  raise GemNotFound, "Could not find gem '#{name}' in the current bundle." unless spec
  if spec.name == 'bundler'
    return File.expand_path('../../../', __FILE__)
  end
  spec.full_gem_path
end
like image 123
ben author Avatar answered Oct 14 '22 21:10

ben author