1

I need to get the current working directory of a gem from within a Rails application.

I currently use

`bundle show foo`.strip

This works great in my environment, but:

  • it's slow because it requires loading a shell
  • it will probably break when somebody tries to run the app on Windows or on JRuby or when their $PATH points at a different ruby than the one used to start the app

So I'd like a way to do this without invoking a subshell.

The RDOC for Bundler hasn't been helpful. You used to be able to get this sort of information from Rails itself in Rails 2, but it appears that Rails 3 lets Bundler handle it.

4

1 回答 1

3

如果 gem 在您的 Gemfile/Gemfile.lock 中,它的路径会出现在$LOAD_PATH全局变量中。你可以利用这个事实:

$:.grep(/activerecord/).first

$:是一样的$LOAD_PATH。你可以使用任何一个。

另一种方法:

Bundler.load.specs.find{|s| s.name == 'activerecord' }.try(:full_gem_path)
于 2012-10-06T12:17:09.557 回答