Rubygem如何需要所有宝石? [英] How does Rubygem require all gems?
问题描述
我对Rubygem感兴趣,并开始研究它是如何工作的,并且发现1.9之后,Rubygem的 require 变成了
require
。
使用下面的代码:
require'debugger'
debugger
require'thor'
我开始 n
和 l
和 s
,但陷入:
#specification.rb:263
def self._all#:nodoc:
除非已定义?( @@ all)&& @@ all then
specs = {}
self.dirs.each {| dir |
Dir [File.join(dir,* .gemspec)]。each {| path |
spec = Gem :: Specification.load path.untaint
#如果spec不好,返回nil,所以我们在这个阶段忽略
#it
specs [spec .full_name] || = spec if spec
}
}
@@ all = specs.values
_resort!
end
@@ all
end
进入上面的方法, @@ all
已经准备好了。然后,我在任何地方设置了断点 @@ all =
,但没有任何一个断点可以达到。
我错过了什么?
编辑:
再看看我的问题。请参阅 require'debugger'
?我感觉就像一个傻瓜。
现在问题是我如何调试 require
?
已关闭:
Plz看到这个很好的答案: https://stackoverflow.com/a/16069229/342366
Thx再次为此问候答案: https:
对于一些Google来说这个问题(和懒惰的调试),我决定发布Rubygem如何要求所有宝石?的答案。
下面是关键步骤:
-
在Ruby193 \lib\ruby\gems\1.9.1\specifications中加载所有.gemspec
Dir [File.join(dir,* .gemspec)]。each {| path |
spec = Gem :: Specification.load path.untaint
#如果spec不好,返回nil,所以我们在这个阶段忽略
#it
specs [spec .full_name] || = spec if spec
}
-
版本desc
@@ all.sort! {| a,b |
names = a.name< => b.name
next names如果names.nonzero?
b.version< => a.version
}
-
获得了第一个gem(更高版本)
self.find {| spec |
spec.contains_requirable_file?路径
}
-
激活gem和所有依赖关系〜大家都很开心。 p>
I got interested in Rubygem, and started to explore how does it works, and found out that after 1.9, Rubygem's require
became THE require
.
With the code below:
require 'debugger'
debugger
require 'thor'
I started to n
and l
and s
, but got stuck at:
# 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
It seems that before stepping into the method above, @@all
has already be been prepared. Then I set break-points everywhere @@all =
, but none of the break-points are reached.
What am i missing???
EDIT:
Look at my question again. See require 'debugger'
? I feel like a fool.
Now the question is "How can I debug require
"?
CLOSED:
Plz see this great answer:https://stackoverflow.com/a/16069229/342366
Thx again for this greet answer:https://stackoverflow.com/a/16069229/342366, i did a little debugging myself.
For some one google to this question(and lazy to debug), i decide to post the answer to "How does Rubygem require all gems?"
There are the key-steps below:
load all ".gemspec" in "Ruby193\lib\ruby\gems\1.9.1\specifications"
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 }
sort the gems by version desc
@@all.sort! { |a, b| names = a.name <=> b.name next names if names.nonzero? b.version <=> a.version }
got the first gem (the higher version)
self.find { |spec| spec.contains_requirable_file? path }
activate the gem and all dependencies~ Everyone's happy.
这篇关于Rubygem如何需要所有宝石?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!