Rubygem如何需要所有宝石?

时间:2013-04-17 14:44:40

标签: ruby rubygems

我对Rubygem感兴趣,并开始探索它是如何工作的,并发现在1.9之后,Rubygem的require变成了require

使用以下代码:

require 'debugger'
debugger
require 'thor'

我开始nl以及s,但却陷入了困境:

  # specification.rb:263
  def self._all # :nodoc:
    unless defined?(@@all) && @@all then
      specs = {}

      self.dirs.each { |dir|
        Dir[File.join(dir, "*.gemspec")].each { |path|
          spec = Gem::Specification.load path.untaint
          # #load returns nil if the spec is bad, so we just ignore
          # it at this stage
          specs[spec.full_name] ||= spec if spec
        }
      }

      @@all = specs.values

      _resort!
    end
    @@all
  end

在进入上述方法之前,似乎已经准备好了@@all。然后我在任何地方设置断点@@all =,但没有到达断点。

我缺少什么?


修改

再看看我的问题。见require 'debugger'?我觉得自己很傻。

现在的问题是“如何调试require


CLOSED:

Plz看到这个很棒的答案:https://stackoverflow.com/a/16069229/342366

1 个答案:

答案 0 :(得分:0)

再次回答这个问候答案:https://stackoverflow.com/a/16069229/342366,我自己做了一点调试。

对于某些谷歌这个问题(并且懒得调试),我决定将答案发布为“Rubygem如何需要所有宝石?”

以下是关键步骤:

  1. 在“Ruby193 \ lib \ ruby​​ \ gems \ 1.9.1 \ specifications”中加载所有“.gemspec”

    Dir[File.join(dir, "*.gemspec")].each { |path|
      spec = Gem::Specification.load path.untaint
      # #load returns nil if the spec is bad, so we just ignore
      # it at this stage
      specs[spec.full_name] ||= spec if spec
    }
    
  2. 按版本desc

    对宝石进行排序
    @@all.sort! { |a, b|
      names = a.name <=> b.name
      next names if names.nonzero?
      b.version <=> a.version
    }
    
  3. 获得第一个宝石(更高版本)

    self.find { |spec|
      spec.contains_requirable_file? path
    }
    
  4. 激活gem和所有依赖项〜每个人都很高兴。