理想的 Ruby 项目结构
我正在概述/澄清 ruby(非rails/merb/等)项目的理想项目结构。 我猜接下来
app/
bin/ #Files for command-line execution
lib/
appname.rb
appname/ #Classes and so on
Rakefile #Running tests
README
test,spec,features/ #Whichever means of testing you go for
appname.gemspec #If it's a gem
是我做错了什么吗? 我错过了哪些部分?
I'm after an overview/clarification of the ideal project structure for a ruby (non-rails/merb/etc) project. I'm guessing it follows
app/
bin/ #Files for command-line execution
lib/
appname.rb
appname/ #Classes and so on
Rakefile #Running tests
README
test,spec,features/ #Whichever means of testing you go for
appname.gemspec #If it's a gem
Have I got something wrong? What parts have I missed out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这非常正确。 默认情况下,Rubygems 会将 lib 目录添加到加载路径,但您可以使用 $: 变量将任何您想要的目录推送到该目录上。 也就是说
,当您在该目录中添加
surfer.rb
时,您可以在任何地方require "surfer"
并且会找到该文件。另外,作为惯例,类和单例获取文件,模块获取目录。 例如,如果您有
LolCatz
模块和LolCatz::Moar
类,则如下所示:这就是为什么有一个 lib/appname 文件夹,因为大多数库都位于
appname
命名空间。此外,如果您尝试运行命令
newgem --simple [projectname]
,它将快速为您生成一个脚手架,其中仅包含 Ruby 项目(以及扩展的 Ruby Gem)的基本要素。 我知道还有其他工具可以做到这一点,但 newgem 很常见。 我通常会删除 TODO 文件和所有脚本内容。I think that is pretty much spot on. By default, Rubygems will add the lib directory to the loadpath, but you can push any directory you want onto that using the $: variable. i.e.
That means when you have say,
surfer.rb
in that dir, you canrequire "surfer"
anywhere and the file will be found.Also, as a convention, classes and singletons get a file and modules get a directory. For instance, if you had the
LolCatz
module and theLolCatz::Moar
class that would look like:That is why there is an lib/appname folder because most libraries are in the
appname
namespace.Additionally, if you try running the command
newgem --simple [projectname]
that'll quickly generate a scaffold for you with just the bare essentials for a Ruby project (and by extension a Ruby Gem). There are other tools which do this, I know, but newgem is pretty common. I usually get rid of the TODO file and all the script stuff.请参阅 http://guides.rubygems.org/what-is-a 中的以下示例-宝石/
See the following example from http://guides.rubygems.org/what-is-a-gem/
我尝试模仿 Rails 项目结构,因为我的团队(通常与 Rails 打交道)会比其他配置更好地理解该结构。 约定优于配置——从 Rails 中渗透出来。
I attempt to mimic the Rails project structure because my team, which usually deals with Rails, will understand the structure better than another configuration. Convention over Configuration - bleeding over from Rails.
如果您使用捆绑器,运行此命令
bundle gem app_name
将为您提供相同的目录结构。如果您想使用 rspec 而不是单元测试,则可以运行此命令
rspec --init
(只需确保先
cd app_name
)If you use bundler, running this command
bundle gem app_name
will give you the same directory structure.If you want to use rspec instead of unit tests you can then run this command
rspec --init
(Just make sure you
cd app_name
first)