如何将 gem 配置为仅使用系统 gem?

发布于 2024-09-19 09:35:45 字数 2111 浏览 3 评论 0原文

我想将 gem 配置为仅使用系统 gem - 而不是 $HOME/.gem 中的那些。这是因为我正在编写一个将访问 Gem.path 的脚本,并且我不希望它返回我的主目录中 gems 的路径。

我很确定我没有在我的 .bashrc.bash_login 等中显式设置 GEM_HOME 或类似的内容。

但是 Gem.path 首先返回我的 homedir gems:

irb
> Gem.path
=> ["/home/nfm/.gem/ruby/1.9.1", "/usr/local/lib/ruby/gems/1.9.1"]

我可以阻止这种情况发生吗?在哪里配置的?或者只是默认首先查看 homedir ?

如果我无法配置此功能,我可以使用 regexp hackery 返回 gems 的系统路径吗?

更多详细信息:

which ruby
/usr/local/bin/ruby
ruby --version
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]

gem env
  - RUBYGEMS VERSION: 1.3.7
  - RUBY VERSION: 1.9.2 (2010-08-18 patchlevel 0) [x86_64-linux]
  - INSTALLATION DIRECTORY: /usr/local/lib/ruby/gems/1.9.1
  - RUBY EXECUTABLE: /usr/local/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/local/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /usr/local/lib/ruby/gems/1.9.1
     - /home/nfm/.gem/ruby/1.9.1
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

更新:

所以显然这可以在~/.gemrc中配置:

# Note the use of a symbol before the colon - the string version doesn't work!
:gempath:
  - /usr/local/lib/ruby/gems/1.9.1

但是,如果您触发,这似乎不会生效上irb。这与配置文件是 YAML 这一事实有关,并且显然在 irb 启动时未加载 yaml(对此不确定!):

$ irb
> Gem.path
=> ["/home/nfm/.gem/ruby/1.9.1", "/usr/local/lib/ruby/gems/1.9.1"]
> Gem.configuration.path
=> ["/usr/local/lib/ruby/gems/1.9.1"]
# Ready for a WTF moment?
> Gem.path
=> ["/usr/local/lib/ruby/gems/1.9.1"]

所以,下面的答案似乎是获得正确行为的唯一一致的方法,即使您假设 ~/.gemrc 可以工作并且是包装配置的更好方法。

但是,在我的 ~/.gemrc 中设置 :gempath: 是在 Rails 应用程序的 Rakefile 中的脚本上下文中进行的,大概是这样因为 yaml 是显式加载的。

不确定 yaml 到底发生了什么,但这个解释似乎与我在这里看到的一致。

改装起来! :P

I want to configure gem to only use system gems - never those in $HOME/.gem. This is because I'm writing a script that will access Gem.path and I don't want it to return the path to gems in my home directory.

I'm pretty sure I haven't explicitly set GEM_HOME or anything like that in my .bashrc, .bash_login etc.

But Gem.path returns my homedir gems first:

irb
> Gem.path
=> ["/home/nfm/.gem/ruby/1.9.1", "/usr/local/lib/ruby/gems/1.9.1"]

Can I stop this from happening? Where is it configured? Or is it just the default to look in homedir first?

If I can't configure this, can I return the system path for gems with regexp hackery?

More details:

which ruby
/usr/local/bin/ruby
ruby --version
ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]

gem env
  - RUBYGEMS VERSION: 1.3.7
  - RUBY VERSION: 1.9.2 (2010-08-18 patchlevel 0) [x86_64-linux]
  - INSTALLATION DIRECTORY: /usr/local/lib/ruby/gems/1.9.1
  - RUBY EXECUTABLE: /usr/local/bin/ruby
  - EXECUTABLE DIRECTORY: /usr/local/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /usr/local/lib/ruby/gems/1.9.1
     - /home/nfm/.gem/ruby/1.9.1
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/

Update:

So apparently this can be configured in ~/.gemrc:

# Note the use of a symbol before the colon - the string version doesn't work!
:gempath:
  - /usr/local/lib/ruby/gems/1.9.1

However, this does not seem to take effect if you fire up irb. This has something to do with the fact that the config file is YAML, and apparently yaml isn't loaded when irb starts (not sure on this one!):

$ irb
> Gem.path
=> ["/home/nfm/.gem/ruby/1.9.1", "/usr/local/lib/ruby/gems/1.9.1"]
> Gem.configuration.path
=> ["/usr/local/lib/ruby/gems/1.9.1"]
# Ready for a WTF moment?
> Gem.path
=> ["/usr/local/lib/ruby/gems/1.9.1"]

So, the answer below seems like the only consistent way to get the correct behavior, even though you'd assume that ~/.gemrc would work and would be a nicer way to wrap you config up.

However, setting :gempath: in my ~/.gemrc worked in the context of the script being in my Rakefile in a Rails app, presumably because yaml is explicitly loaded.

Not sure exactly what's going on with yaml, but this explanation seems consistent with what I'm seeing here.

Mod up! :P

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

街角迷惘 2024-09-26 09:35:45

在这里覆盖它:

#in ~/.bashrc
export GEM_PATH=/usr/local/lib/ruby/gems/1.9.1

overwrite it here:

#in ~/.bashrc
export GEM_PATH=/usr/local/lib/ruby/gems/1.9.1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文