【问题标题】:How does Rubygem require all gems? [duplicate]Rubygem 如何需要所有宝石? [复制]
【发布时间】:2013-04-17 14:44:39
【问题描述】:

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

使用下面的代码:

require 'debugger'
debugger
require 'thor'

我开始使用nls,但卡在:

  # 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


关闭:

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

【问题讨论】:

  • 你说的«我开始n && l && s»是什么意思? self._all 来自哪里?
  • 我试图澄清你的问题,如果你不同意,请帮助自己......
  • @BeatRichartz n&&l&&s == next&&list&&step,调试器中的命令
  • "next&&list&&step" 那么请把这些拼写出来。使用自己的速记只会让不懂的人感到困惑。请记住,世界各地的人们都会访问 Stack Overflow。
  • 如果问题发生了变化,请打开一个新问题。您的问题的标题和正文与“我如何调试require”几乎没有关系,这个问题值得拥有自己的空间。

标签: ruby rubygems


【解决方案1】:

再次感谢这个问候答案: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. 按版本描述对 gem 进行排序

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

    self.find { |spec|
      spec.contains_requirable_file? path
    }
    
  4. 激活gem和所有依赖~大家都很开心。

【讨论】:

    猜你喜欢
    • 2018-09-24
    • 2013-01-18
    • 2011-02-12
    • 1970-01-01
    • 2015-08-04
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 2017-04-18
    相关资源
    最近更新 更多