JayTarka
JayTarka

Reputation: 571

Find a gem's root

I have 2 gems, gemA and gemB. I want gemA to be installed into B and I want A to be able to manipulate B's filing system.

The first step to this process is finding gemB 's root.

In gemA:

module GemA
    def self.manipulate_B
         puts __dir__
    end
end

In gemB:

module GemB
    def self.get_manipulated_by_a
         GemA.manipulate_B
    end
end

Upon running GemB.get_manipulated_by_a I get this path:

/home/jay/.rvm/gems/ruby-2.2.1/gems/gem_a-0.1.0/lib/gem_a/

I get the near root of gemA, installed as a gem. Way, way off what I want. I want

/home/jay/Documents/Projects/gem_b

and of course, if gemA was installed in gemN

/home/jay/Documents/Projects/gem_n

Update

I should have said, but GemB is being developed in Documents and GemA is an installed gem to help with the development of GemB. GemB is not installed. I certainly do not want to modify the contents of an installed gem! If you look at my paths you can see what I mean!

Also, this must be possible because Rails.root does it...maybe I'll look at some sauce...

Upvotes: 2

Views: 82

Answers (1)

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34338

In general, you could use the following command to get the installation location of a gem:

gem list <gem_name> -d

Or, if you're using bundler, then you could also use:

bundle show <gem_name>

For example, to see the location of rspec gem, I do:

➜  gem list rspec -d

*** LOCAL GEMS ***

rspec (3.2.0)
    Authors: Steven Baker, David Chelimsky, Myron Marston
    Homepage: http://github.com/rspec
    License: MIT
    Installed at: /Users/rislam/.rvm/gems/ruby-2.2.1

    rspec-3.2.0

Another way to find the location of a gem is to use Gem::Specification.find_by_name method like this:

2.2.1 :006 > spec = Gem::Specification.find_by_name("rspec")
 => #<Gem::Specification:0x3fd94f85bd04 rspec-3.2.0> 
2.2.1 :007 > spec.gem_dir
 => "/Users/rislam/.rvm/gems/ruby-2.2.1/gems/rspec-3.2.0" 

Upvotes: 3

Related Questions