When using `bundle gem floob`, why is a directory of the same name as the gem is created?

血红的双手。 提交于 2020-01-06 06:07:47

问题


I am making my first gem. Let's assumed it is called floob. ex: bundle gem floob

Why would bundle gem create two directories of the same name for a gem? ex: /floob/lib/floob/

Do I put the bulk of my code in /lib/floob/ (alongside the version.rb file?)

What is the purpose of the file that is created with the gem name? ex: /floob/lib/floob.rb

The only command run was bundle gem.

I would love a little clarification on what the relationship is between all the floobs~


回答1:


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.

This way if you put floob.rb into /floob/lib, you can load it with:

require '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..

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.

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.

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.



来源:https://stackoverflow.com/questions/50299899/when-using-bundle-gem-floob-why-is-a-directory-of-the-same-name-as-the-gem-is

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!