How to find the path where Ruby Gem is installed (i.e. Gem.lib_path cf Gem.bin_path)

Gem.bin_path('cucumber', 'cucumber') 

Will return a binary / executable path. There seems to be no function to return the library path. That in this case, ideally, will return:

 /home/hedge/.rvm/gems/ruby-1.9.2-p136@bbb-bdd-meta-bdd/gems/cucumber-0.10.0/lib 

Am I missing something or is there a simple / only way to get this information?

Updated: no CLI or non-stdlib clauses.

+42
ruby api rubygems
Mar 07 2018-11-11T00:
source share
4 answers

The problem with the verified answer is that you have to “require” rubygem, otherwise it will not work. This is often undesirable because if you are working with an executable stone, you do not want to “require” it, or you will receive a bunch of warnings.

This is a universal solution for executables and libs:

 spec = Gem::Specification.find_by_name("cucumber") gem_root = spec.gem_dir gem_lib = gem_root + "/lib" 

If you want to get really technical, there is more than one lib directory. Gemspec has an array of "require_paths" for all directories to search (added to $ LOAD_PATH). So, if you need an array of require_paths, use this:

 gem_lib = gem_root + "/" + spec.require_paths[0] 

No need to bind.

+72
Apr 10
source share

After the stone has been loaded with the request, you will find the lib path using Gem.loaded_specs, as follows:

 require 'rubygems' require 'cucumber' gem_root = Gem.loaded_specs['cucumber'].full_gem_path gem_lib = File.join(gem_root, 'lib') 
+23
Apr 6 2018-11-11T00:
source share

I'm not sure why you are doing this, but if you are on the command line use gem env .

+15
Mar 07 2018-11-23T00:
source share

Try using bundle show cucumber .

Which, looking at the source of the bundle, does something like:

 spec = Bundler.load.specs.find{|s| s.name == name } spec.full_gem_path 

You are using bundler, right?

+4
Mar 07 '11 at 23:45
source share



All Articles