问题描述
我正在做我的第一颗宝石。假设它叫做 floob
。例如:套装宝石吊坠
I am making my first gem. Let's assumed it is called floob
. ex: bundle gem floob
为什么套装宝石
为宝石创建两个具有相同名称的目录?例如: / floob / lib / floob /
Why would bundle gem
create two directories of the same name for a gem? ex: /floob/lib/floob/
我是否将大部分代码放在<$ c $中c> / lib / floob / (连同 version.rb
文件?)
Do I put the bulk of my code in /lib/floob/
(alongside the version.rb
file?)
使用gem名称创建的文件的用途是什么?
ex: /floob/lib/floob.rb
What is the purpose of the file that is created with the gem name?ex: /floob/lib/floob.rb
唯一的命令运行是捆绑宝石
。
我想澄清一下所有 Floobs
〜
推荐答案
这种两层结构的原因是, / floob / lib
最终会是添加到Ruby require
加载路径的文件夹。
The reason for this "two-tier" structure is because /floob/lib
will ultimately be the folder that is added to the Ruby require
load path.
这样,如果将 floob.rb
放入 / floob / lib
,则可以加载:
This way if you put floob.rb
into /floob/lib
, you can load it with:
require 'floob'
这也使您有机会在<$ c下将子组件分开,例如 version.rb
$ c> floob 命名空间,方法是将它们放入 / floob / lib / floob
。这意味着您现在可以执行以下操作:
This also gives the opportunity to separate subcomponents, like version.rb
, under the floob
namespace by putting them into /floob/lib/floob
. That means you can now do things like:
require 'floob/version'
require 'floob/base'
require 'floob/core'
etc..
如果 / floob
直接添加到了加载路径,那么很难将代码与 README
文件,gemspecs或其他文件分开资产,例如图像,二进制Blob,YAML设置文件或 bin
存根。
If /floob
was directly added to the load path, then it would be difficult to separate code from things like README
files, gemspecs, or other assets like images, binary blobs, YAML settings files or bin
stubs.
现在,实际代码将位于 lib
,其他所有内容都可以很好地放在 / floob
下的文件夹中,而不会干扰代码或加载路径功能
Now the actual code will live under lib
, and everything else can be put nicely into folders directly under /floob
, without interfering with the code or load path functionality.
还请注意,在大多数情况下,除了 floob.rb
之外,您都不应将其他内容放入 / floob / lib
,因为否则会污染全局加载路径名称空间。相反,应将其余所有代码放入 / floob / lib / floob
中,然后将 require
代码放入像我上面所述,放入 floob.rb
加载那些文件。
Also note that in most cases you should not put anything else than floob.rb
into /floob/lib
, because otherwise you will be polluting the global load path namespace. Instead all the rest of the code should be put into /floob/lib/floob
, and then you put require
code into floob.rb
to load those files, like I described above.
这样可以保持加载路径整洁,并且组件彼此之间很好地分开。
This way you keep the load path clean, and components nicely separated from each other.
这篇关于使用`bundle gem floob`时,为什么会创建一个与gem同名的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!