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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With