【问题标题】:`Gem.find_files` interacts with itself`Gem.find_files` 与自身交互
【发布时间】:2014-12-27 11:25:56
【问题描述】:

假设我没有安装某个 gem,比如说"oj"。如果我执行以下操作,则会安装 gem,最后的 Gem.find_files 返回它的安装路径。

require "open3"
name = "oj"
_, out, err, thread = Open3.popen3("sudo gem install #{name}")
thread.join
puts out.read, err.read
p Gem.find_files(name)

但是,如果我(卸载 gem 并)运行以下命令,在最后发生相同的调用之前有一个额外的 Gem.find_files(name) 行,

require "open3"
name = "oj"
if Gem.find_files(name).empty?
  _, out, err, thread = Open3.popen3("sudo gem install #{name}")
  thread.join
  puts out.read, err.read
  p Gem.find_files(name)
end

然后 Gem.find_files 最后返回一个空数组。似乎Gem.find_files 的第一次调用(安装前)使Gem.find_files 的第二次调用(安装后)返回一个空数组。

  • 为什么它返回一个空数组?
  • 如何修复Gem.find_files 的一个或两个出现,以便第一个(安装前)返回一个空数组,而后一个(安装后)返回包含的路径?

【问题讨论】:

    标签: ruby gem


    【解决方案1】:

    第一次调用find_files 时,Gem 代码会缓存一堆信息,包括当前的 gem 规范及其目录。

    第二次调用find_files,Gem 代码使用相同的缓存信息,因此不知道添加了新的 gem。

    解决方案是安装新的 gem,然后像这样重置缓存:

    Gem::Specification.reset
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-08
      • 1970-01-01
      • 2011-05-23
      • 2011-10-06
      • 1970-01-01
      • 1970-01-01
      • 2013-09-20
      • 2012-08-09
      相关资源
      最近更新 更多