Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Ruby project root path from a Gem

I've started to build a Gem by my self.

Now from my Gem I need get the root path from the ruby project what are using it.

Thank you.

like image 875
Henrique Soares Avatar asked Mar 10 '23 21:03

Henrique Soares


2 Answers

Yes you can using the gem list command

Here an example.

$ gem list green_shoes -d

*** LOCAL GEMS ***

green_shoes (1.1.374)
    Author: ashbb
    Homepage: http://github.com/ashbb/green_shoes
    Installed at: d:/Ruby/lib/ruby/gems/2.3.0

    Green Shoes

From withing a Ruby gem this can be done like this

spec = Gem::Specification.find_by_name("green_shoes")
puts spec.gem_dir

# d:/Ruby/lib/ruby/gems/2.3.0/gems/green_shoes-1.1.374
like image 43
peter Avatar answered Mar 16 '23 11:03

peter


Check out this forum: https://www.ruby-forum.com/topic/143383 From my understanding of your question, you want to get the path of the project NOT the path of where the gem itself is located.

class MyGem

   def self.get_working_dir
     Dir.pwd
   end

end

> MyGem.get_working_dir
> /Users/your_name/my_project_directory

This solution ended up helping out a Gem I had made where I had to iterate through a project's file tree.

like image 145
tqr_aupa_atleti Avatar answered Mar 16 '23 12:03

tqr_aupa_atleti