纯Ruby项目的目录布局

我开始学习ruby了。 我也是一个日常的C ++开发者。 对于C ++项目,我通常使用以下dir结构

/ -/bin <- built binaries -/build <- build time temporary object (eg. .obj, cmake intermediates) -/doc <- manuals and/or Doxygen docs -/src --/module-1 --/module-2 -- non module specific sources, like main.cpp - IDE project files (.sln), etc. 

Ruby(非Rails,非Merb)的dir布局会建议保持干净,简单和可维护吗?

Bundler包含生成gem的必要基础结构:

 $ bundle gem --coc --mit --test=minitest --exe spider Creating gem 'spider'... MIT License enabled in config Code of conduct enabled in config create spider/Gemfile create spider/lib/spider.rb create spider/lib/spider/version.rb create spider/spider.gemspec create spider/Rakefile create spider/README.md create spider/bin/console create spider/bin/setup create spider/.gitignore create spider/.travis.yml create spider/test/test_helper.rb create spider/test/spider_test.rb create spider/LICENSE.txt create spider/CODE_OF_CONDUCT.md create spider/exe/spider Initializing git repo in /Users/francois/Projects/spider Gem 'spider' was successfully created. For more information on making a RubyGem visit https://bundler.io/guides/creating_gem.html 

然后,在lib /中,根据需要创建模块:

 lib/ spider/ base.rb crawler/ base.rb spider.rb require "spider/base" require "crawler/base" 

有关--coc , – --exe--mit选项的详细信息,请阅读bundle gem的手册页。

截至2011年,通常使用珠宝商代替newgem,因为后者被有效放弃。

标准Ruby项目的核心结构基本上是:

  lib/ foo.rb foo/ share/ foo/ test/ helper.rb test_foo.rb HISTORY.md (or CHANGELOG.md) LICENSE.txt README.md foo.gemspec 

share/很少见,有时也称为data/ 。 它用于通用非ruby文件。 大多数项目都不需要它,但即使它们多次执行,所有内容都只保留在lib/ ,尽管这可能不是最佳实践。

test/目录可能被称为spec/如果正在使用BDD而不是TDD,尽管您可能还会看到features/如果使用Cucumber,或者demo/如果使用QED。

这些天foo.gemspec可以只是.gemspec特别是如果它不是手动维护。

如果您的项目有命令行可执行文件,则添加:

  bin/ foo man/ foo.1 foo.1.md or foo.1.ronn 

此外,大多数Ruby项目都有:

  Gemfile Rakefile 

Gemfile用于使用Bundler, Rakefile用于Rake构建工具。 但如果您想使用不同的工具,还有其他选择。

其他一些不那么罕见的文件:

  VERSION MANIFEST 

VERSION文件只包含当前版本号。 MANIFEST (或Manifest.txt )包含要包含在项目包文件中的文件列表(例如gem包)。

你可能会看到什么,但使用是零星的:

  config/ doc/ (or docs/) script/ log/ pkg/ task/ (or tasks/) vendor/ web/ (or site/) 

config/包含各种配置文件; doc/包含生成的文档,例如RDoc,或者有时手动维护的文档; script/包含供项目使用的shell脚本; log/包含生成的项目日志,例如测试覆盖率报告; pkg/保存生成的包文件,例如foo-1.0.0.gem ; task/可以保存各种任务文件,如foo.rakefoo.watchr ; vendor/包含其他项目的副本,例如git子模块; 最后是web/包含项目的网站文件。

然后一些工具特定的文件也相对常见:

  .document .gitignore .yardopts .travis.yml 

它们相当不言自明。

最后,我将添加我个人添加一个.index文件和一个var/目录来构建该文件(搜索“Rubyworks Indexer”以获取更多相关信息)并且通常有一个work目录,例如:

  work/ NOTES.md consider/ reference/ sandbox/ 

只是用于开发目的的scrapyard。

@ Dentharg :你的“包含一个包含所有子部分”是一种常见的模式。 像任何东西一样,它有它的优点(很容易得到你想要的东西)和它的缺点(许多包括可以污染命名空间,你无法控制它们)。 您的模式如下所示:

 - src/ some_ruby_file.rb: require 'spider' Spider.do_something + doc/ - lib/ - spider/ spider.rb: $: << File.expand_path(File.dirname(__FILE__)) module Spider # anything that needs to be done before including submodules end require 'spider/some_helper' require 'spider/some/other_helper' ... 

我可能会建议这样做以允许更多控制:

 - src/ some_ruby_file.rb: require 'spider' Spider.include_all Spider.do_something + doc/ - lib - spider/ spider.rb: $: << File.expand_path(File.dirname(__FILE__)) module Spider def self.include_all require 'spider/some_helper' require 'spider/some/other_helper' ... end end 

为什么不使用相同的布局? 通常你不需要构建因为没有编译步骤,但其余的对我来说似乎没问题。

我不确定你的模块是什么意思,但如果它只是一个单独的类,则不需要单独的文件夹,如果有多个文件,你通常会编写一个module-1.rb文件(在名称级别为module-1文件夹)只需要模块-1 /中的所有内容。

哦,我建议使用Rake来管理任务(而不是make)。

我会坚持类似于你熟悉的东西:在你自己的项目目录中做一个陌生人是没有意义的。 🙂

我一直有的典型事情是lib | src,bin,test。

(我不喜欢这些怪物生成器:我想用新项目做的第一件事就是得到一些代码,而不是写自述文件,文档等等!)

所以我选择了newgem。 我删除了所有不必要的RubyForge / gem东西(锄头,设置等),创建了git repo,将导入的项目导入NetBeans。 全部用了20分钟,一切都是绿色的。 这甚至给了我一个spec文件的基本rake任务。

谢谢你们。