简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM