I'm starting to learn ruby. I'm also a day-to-day C++ dev. For C++ projects I usually go with following dir structure
我开始学习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.
What dir layout for Ruby (non-Rails, non-Merb) would you suggest to keep it clean, simple and maintainable?
对于Ruby(非rails、非merb)的dir布局,您会建议保持它干净、简单和可维护吗?
7 个解决方案
#1
11
You could install the newgem RubyGem and let it generate the layout for you.
您可以安装newgem RubyGem,并让它为您生成布局。
$ gem install newgem
$ newgem spider
create
create config
create doc
create lib
create script
create tasks
create lib/spider
create History.txt
create License.txt
create Rakefile
create README.txt
create PostInstall.txt
create setup.rb
create lib/spider.rb
create lib/spider/version.rb
create config/hoe.rb
create config/requirements.rb
create tasks/deployment.rake
create tasks/environment.rake
create tasks/website.rake
dependency install_test_unit
create test
create test/test_helper.rb
create test/test_spider.rb
dependency install_website
create website/javascripts
create website/stylesheets
exists script
exists tasks
create website/index.txt
create website/index.html
create script/txt2html
force tasks/website.rake
dependency plain_theme
exists website/javascripts
exists website/stylesheets
create website/template.html.erb
create website/stylesheets/screen.css
create website/javascripts/rounded_corners_lite.inc.js
dependency install_rubigen_scripts
exists script
create script/generate
create script/destroy
create script/console
create Manifest.txt
readme readme
Important
=========
* Open config/hoe.rb
* Update missing details (gem description, dependent gems, etc.)
Then, in lib/, you create modules as needed:
然后,在lib/中,根据需要创建模块:
lib/
spider/
base.rb
crawler/
base.rb
spider.rb
require "spider/base"
require "crawler/base"
#2
20
As of 2011, it is common to use jeweler instead of newgem as the latter is effectively abandoned.
从2011年开始,人们普遍使用珠宝商而不是newgem,因为后者实际上已经被抛弃了。
#3
10
The core structure of a standard Ruby project is basically:
标准Ruby项目的核心结构基本上是:
lib/
foo.rb
foo/
share/
foo/
test/
helper.rb
test_foo.rb
HISTORY.md (or CHANGELOG.md)
LICENSE.txt
README.md
foo.gemspec
The share/
is rare and is sometimes called data/
instead. It is for general purpose non-ruby files. Most projects don't need it, but even when they do many times everything is just kept in lib/
, though that is probably not best practice.
这种共享/是罕见的,有时被称为data/而不是data。它是通用的非ruby文件。大多数项目不需要它,但是即使他们做了很多次,所有的事情都被保存在lib/中,尽管这可能不是最佳实践。
The test/
directory might be called spec/
if BDD is being used instead of TDD, though you might also see features/
if Cucumber is used, or demo/
if QED is used.
如果使用的是BDD而不是TDD,那么测试/目录可能会被称为spec/目录,尽管您可能还会看到特性/如果使用了Cucumber,或者使用了demo/ if QED。
These days foo.gemspec
can just be .gemspec
--especially if it is not manually maintained.
这些天foo。gemspec可以是。gemspec——尤其是如果它不是手工维护的话。
If your project has command line executables, then add:
如果您的项目有命令行可执行文件,则添加:
bin/
foo
man/
foo.1
foo.1.md or foo.1.ronn
In addition, most Ruby project's have:
此外,大多数Ruby项目都有:
Gemfile
Rakefile
The Gemfile
is for using Bundler, and the Rakefile
is for Rake build tool. But there are other options if you would like to use different tools.
Gemfile用于使用Bundler, Rakefile用于Rake构建工具。但是如果您想使用不同的工具,还有其他的选择。
A few other not-so-uncommon files:
其他一些不太常见的文件:
VERSION
MANIFEST
The VERSION
file just contains the current version number. And the MANIFEST
(or Manifest.txt
) contains a list of files to be included in the project's package file(s) (e.g. gem package).
版本文件只包含当前版本号。而MANIFEST(或MANIFEST .txt)包含项目包文件(例如gem包)中包含的文件列表。
What else you might see, but usage is sporadic:
您可能还会看到什么,但使用是零星的:
config/
doc/ (or docs/)
script/
log/
pkg/
task/ (or tasks/)
vendor/
web/ (or site/)
Where config/
contains various configuration files; doc/
contains either generated documentation, e.g. RDoc, or sometimes manually maintained documentation; script/
contains shell scripts for use by the project; log/
contains generated project logs, e.g. test coverage reports; pkg/
holds generated package files, e.g. foo-1.0.0.gem
; task/
could hold various task files such as foo.rake
or foo.watchr
; vendor/
contains copies of the other projects, e.g. git submodules; and finally web/
contains the project's website files.
其中配置/包含各种配置文件;doc/包含生成的文档,例如RDoc,有时也包含手动维护的文档;脚本/包含项目使用的shell脚本;日志/包含生成的项目日志,例如测试覆盖率报告;pkg/保存生成的包文件,例如foo-1.0. gem;任务/可以保存各种任务文件,如foo。rake或foo.watchr;供应商/包含其他项目的副本,例如git子模块;最后是web/包含项目的网站文件。
Then some tool specific files that are also relatively common:
然后一些工具特定的文件也比较常见:
.document
.gitignore
.yardopts
.travis.yml
They are fairly self-explanatory.
他们基本是不言自明的。
Finally, I will add that I personally add a .index
file and a var/
directory to build that file (search for "Rubyworks Indexer" for more about that) and often have a work
directory, something like:
最后,我将添加一个.index文件和一个var/目录来构建这个文件(搜索“Rubyworks Indexer”以获得更多信息),并且通常有一个工作目录,比如:
work/
NOTES.md
consider/
reference/
sandbox/
Just sort of a scrapyard for development purposes.
只是为了开发的目的。
#4
2
@Dentharg: your "include one to include all sub-parts" is a common pattern. Like anything, it has its advantages (easy to get the things you want) and its disadvantages (the many includes can pollute namespaces and you have no control over them). Your pattern looks like this:
@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'
...
I might recommend this to allow a little more control:
我可能会建议你这样做,这样可以有更多的控制:
- 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
#5
1
Why not use just the same layout? Normally you won't need build because there's no compilation step, but the rest seems OK to me.
为什么不使用相同的布局呢?通常您不需要编译,因为没有编译步骤,但是我觉得其余的都没问题。
I'm not sure what you mean by a module but if it's just a single class a separate folder wouldn't be necessary and if there's more than one file you normally write a module-1.rb file (at the name level as the module-1 folder) that does nothing more than require everything in module-1/.
我不确定你所说的模块是什么意思,但是如果它只是一个类,就不需要单独的文件夹,如果有多个文件,你通常会编写一个模块-1。rb文件(在名称层作为模块1文件夹),它只需要模块1/中的所有内容。
Oh, and I would suggest using Rake for the management tasks (instead of make).
哦,我建议把Rake用于管理任务(而不是make)。
#6
0
I would stick to something similar to what you are familiar with: there's no point being a stranger in your own project directory. :-)
我将坚持类似于您所熟悉的内容:在您自己的项目目录中作为一个陌生人是没有意义的。:-)
Typical things I always have are lib|src, bin, test.
我通常有lib|src, bin, test。
(I dislike these monster generators: the first thing I want to do with a new project is get some code down, not write a README, docs, etc.!)
(我不喜欢这些怪物生成器:我要做的第一件事就是把一些代码写下来,而不是写自述、文档等等!)
#7
0
So I went with newgem. I removed all unnecessary RubyForge/gem stuff (hoe, setup, etc.), created git repo, imported project into NetBeans. All took 20 minutes and everything's on green. That even gave me a basic rake task for spec files.
所以我选择了newgem。我去掉了所有不必要的RubyForge/gem (hoe, setup等),创建了git repo,将项目导入到NetBeans中。都花了20分钟,一切都是绿色的。这甚至给了我一个规范文件的基本rake任务。
Thank you all.
谢谢大家。
#1
11
You could install the newgem RubyGem and let it generate the layout for you.
您可以安装newgem RubyGem,并让它为您生成布局。
$ gem install newgem
$ newgem spider
create
create config
create doc
create lib
create script
create tasks
create lib/spider
create History.txt
create License.txt
create Rakefile
create README.txt
create PostInstall.txt
create setup.rb
create lib/spider.rb
create lib/spider/version.rb
create config/hoe.rb
create config/requirements.rb
create tasks/deployment.rake
create tasks/environment.rake
create tasks/website.rake
dependency install_test_unit
create test
create test/test_helper.rb
create test/test_spider.rb
dependency install_website
create website/javascripts
create website/stylesheets
exists script
exists tasks
create website/index.txt
create website/index.html
create script/txt2html
force tasks/website.rake
dependency plain_theme
exists website/javascripts
exists website/stylesheets
create website/template.html.erb
create website/stylesheets/screen.css
create website/javascripts/rounded_corners_lite.inc.js
dependency install_rubigen_scripts
exists script
create script/generate
create script/destroy
create script/console
create Manifest.txt
readme readme
Important
=========
* Open config/hoe.rb
* Update missing details (gem description, dependent gems, etc.)
Then, in lib/, you create modules as needed:
然后,在lib/中,根据需要创建模块:
lib/
spider/
base.rb
crawler/
base.rb
spider.rb
require "spider/base"
require "crawler/base"
#2
20
As of 2011, it is common to use jeweler instead of newgem as the latter is effectively abandoned.
从2011年开始,人们普遍使用珠宝商而不是newgem,因为后者实际上已经被抛弃了。
#3
10
The core structure of a standard Ruby project is basically:
标准Ruby项目的核心结构基本上是:
lib/
foo.rb
foo/
share/
foo/
test/
helper.rb
test_foo.rb
HISTORY.md (or CHANGELOG.md)
LICENSE.txt
README.md
foo.gemspec
The share/
is rare and is sometimes called data/
instead. It is for general purpose non-ruby files. Most projects don't need it, but even when they do many times everything is just kept in lib/
, though that is probably not best practice.
这种共享/是罕见的,有时被称为data/而不是data。它是通用的非ruby文件。大多数项目不需要它,但是即使他们做了很多次,所有的事情都被保存在lib/中,尽管这可能不是最佳实践。
The test/
directory might be called spec/
if BDD is being used instead of TDD, though you might also see features/
if Cucumber is used, or demo/
if QED is used.
如果使用的是BDD而不是TDD,那么测试/目录可能会被称为spec/目录,尽管您可能还会看到特性/如果使用了Cucumber,或者使用了demo/ if QED。
These days foo.gemspec
can just be .gemspec
--especially if it is not manually maintained.
这些天foo。gemspec可以是。gemspec——尤其是如果它不是手工维护的话。
If your project has command line executables, then add:
如果您的项目有命令行可执行文件,则添加:
bin/
foo
man/
foo.1
foo.1.md or foo.1.ronn
In addition, most Ruby project's have:
此外,大多数Ruby项目都有:
Gemfile
Rakefile
The Gemfile
is for using Bundler, and the Rakefile
is for Rake build tool. But there are other options if you would like to use different tools.
Gemfile用于使用Bundler, Rakefile用于Rake构建工具。但是如果您想使用不同的工具,还有其他的选择。
A few other not-so-uncommon files:
其他一些不太常见的文件:
VERSION
MANIFEST
The VERSION
file just contains the current version number. And the MANIFEST
(or Manifest.txt
) contains a list of files to be included in the project's package file(s) (e.g. gem package).
版本文件只包含当前版本号。而MANIFEST(或MANIFEST .txt)包含项目包文件(例如gem包)中包含的文件列表。
What else you might see, but usage is sporadic:
您可能还会看到什么,但使用是零星的:
config/
doc/ (or docs/)
script/
log/
pkg/
task/ (or tasks/)
vendor/
web/ (or site/)
Where config/
contains various configuration files; doc/
contains either generated documentation, e.g. RDoc, or sometimes manually maintained documentation; script/
contains shell scripts for use by the project; log/
contains generated project logs, e.g. test coverage reports; pkg/
holds generated package files, e.g. foo-1.0.0.gem
; task/
could hold various task files such as foo.rake
or foo.watchr
; vendor/
contains copies of the other projects, e.g. git submodules; and finally web/
contains the project's website files.
其中配置/包含各种配置文件;doc/包含生成的文档,例如RDoc,有时也包含手动维护的文档;脚本/包含项目使用的shell脚本;日志/包含生成的项目日志,例如测试覆盖率报告;pkg/保存生成的包文件,例如foo-1.0. gem;任务/可以保存各种任务文件,如foo。rake或foo.watchr;供应商/包含其他项目的副本,例如git子模块;最后是web/包含项目的网站文件。
Then some tool specific files that are also relatively common:
然后一些工具特定的文件也比较常见:
.document
.gitignore
.yardopts
.travis.yml
They are fairly self-explanatory.
他们基本是不言自明的。
Finally, I will add that I personally add a .index
file and a var/
directory to build that file (search for "Rubyworks Indexer" for more about that) and often have a work
directory, something like:
最后,我将添加一个.index文件和一个var/目录来构建这个文件(搜索“Rubyworks Indexer”以获得更多信息),并且通常有一个工作目录,比如:
work/
NOTES.md
consider/
reference/
sandbox/
Just sort of a scrapyard for development purposes.
只是为了开发的目的。
#4
2
@Dentharg: your "include one to include all sub-parts" is a common pattern. Like anything, it has its advantages (easy to get the things you want) and its disadvantages (the many includes can pollute namespaces and you have no control over them). Your pattern looks like this:
@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'
...
I might recommend this to allow a little more control:
我可能会建议你这样做,这样可以有更多的控制:
- 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
#5
1
Why not use just the same layout? Normally you won't need build because there's no compilation step, but the rest seems OK to me.
为什么不使用相同的布局呢?通常您不需要编译,因为没有编译步骤,但是我觉得其余的都没问题。
I'm not sure what you mean by a module but if it's just a single class a separate folder wouldn't be necessary and if there's more than one file you normally write a module-1.rb file (at the name level as the module-1 folder) that does nothing more than require everything in module-1/.
我不确定你所说的模块是什么意思,但是如果它只是一个类,就不需要单独的文件夹,如果有多个文件,你通常会编写一个模块-1。rb文件(在名称层作为模块1文件夹),它只需要模块1/中的所有内容。
Oh, and I would suggest using Rake for the management tasks (instead of make).
哦,我建议把Rake用于管理任务(而不是make)。
#6
0
I would stick to something similar to what you are familiar with: there's no point being a stranger in your own project directory. :-)
我将坚持类似于您所熟悉的内容:在您自己的项目目录中作为一个陌生人是没有意义的。:-)
Typical things I always have are lib|src, bin, test.
我通常有lib|src, bin, test。
(I dislike these monster generators: the first thing I want to do with a new project is get some code down, not write a README, docs, etc.!)
(我不喜欢这些怪物生成器:我要做的第一件事就是把一些代码写下来,而不是写自述、文档等等!)
#7
0
So I went with newgem. I removed all unnecessary RubyForge/gem stuff (hoe, setup, etc.), created git repo, imported project into NetBeans. All took 20 minutes and everything's on green. That even gave me a basic rake task for spec files.
所以我选择了newgem。我去掉了所有不必要的RubyForge/gem (hoe, setup等),创建了git repo,将项目导入到NetBeans中。都花了20分钟,一切都是绿色的。这甚至给了我一个规范文件的基本rake任务。
Thank you all.
谢谢大家。