是否有组织多文件 Ruby 程序的标准或常规系统?我已经开始用 Ruby 启动我的第一个大型“项目”,这是一个按逻辑组织为多个文件和目录的搜索程序。我在下面概述了我的潜在结构:
- 主文件。该文件实现搜索类和通用搜索协议。
- 算法分析文件。该文件实现解释搜索结果的函数。
- 协议目录
- 提要目录
- 该程序的部分目的是搜索存档的新闻源。此功能的文件位于此文件夹中。
然而,代码当前有一个类(我们称之为 Searcher),每个协议的搜索类都继承自该类(GoogleSearcher < Searcher)。为了管理这个问题,我需要将主文件包含在这些协议文件中(对吗?),考虑到我的理想结构,这似乎是不可能的。
除了我的具体示例之外,我想知道是否有任何约定,例如“更多文件而不是更少”或“文件的逻辑结构是不必要的”。拥有一个“辅助”函数文件(例如在 Rails 中)是否很常见?什么级别的抽象被认为是合适的?
最后,我计划有一天将其作为一个库集成到 Rails 中(不是插件;我希望它也能独立工作)。不知道会不会对组织造成影响。
我知道这是一个非常开放式的问题,但那是因为我很感激任何相关的建议。提前致谢。
Is there a standard or conventional system for organizing multi-file Ruby programs? I have embarked on my first large "project" in Ruby, a search program which is logically organized into multiple files and directories. I've outlined below my potential structure:
- The main file. This file implements the search class and general searching protocol.
- The algorithmic analysis file. This file implements functions that interpret search results.
- Protocols directory
- Contains one file per protocol to search.
- Feeds Directory
- Part of the program's purpose is to search archived news feeds. The files for this feature go in this folder.
However, the code currently has a class (let's call it Searcher) that each protocol's search class inherits from (GoogleSearcher < Searcher). In order to manage this, I need to include the main file in these protocol files (right?) which doesn't seem possible given my ideal structure.
Aside from my specific example, I was wondering if there are any conventions, such as "more files rather than less", or "logical structuring of files is unnecessary". Is it common to have a file of "helper" functions (such as in Rails?) What level of abstraction is considered appropriate?
Finally, I'm planning on integrating this into Rails someday as a library (not a plugin; I want it to work standalone as well). I don't know if this would affect the organization.
I know this is a pretty open-ended question, but that's because I would appreciate any advice that is remotely relevant. Thanks in advance.
发布评论
评论(3)
您可能需要考虑为您的图书馆创建一个 gem。这将使该库无论是独立使用还是与 Rails 一起使用都变得很容易,并且使部署/更新更简单。
此外,由于 gem 通常遵循特定的目录结构,因此它还解决了您不知道如何组织库的问题。
有大量可用于创建 gem 的文档。 这里还有有关文件结构的更多信息以及其他有用的提示。
You may want to consider creating a gem for your library. This would make it easy to use the library both stand-alone and with Rails, as well as make deployment/updates simpler.
Further, because gems normally follow a specific directory structure, it also solves your issue of not knowing how to organize the library.
There is plenty of documentation available for creating gems. Here's a bit more info about file structure, as well as other useful tips.
除了 vonconrad 的答案中推荐的链接外,您还可以参考 编程 Ruby 1.9,作者:Dave Thomas 等人。有一个本书该部分的免费示例 PDF。
本章提到:
Besides the recommended links in vonconrad's Answer, you can refer to the section “Organizing your source” in Chapter 16 of Programing Ruby 1.9 by Dave Thomas et al. There is a free sample PDF of that part of the book.
The chapter mentions:
我知道这个问题已经很老了,但希望对后来的人有用...
我同意上面所说的:gems 是组织和重用代码的好方法。
除了上面的链接之外,我是否可以建议使用 Bundler 来创建 gem,如下所示
在 Ryan Bates RailsCast 中: http://railscasts.com/episodes/245-new -gem-with-bundler
我发现 Bundler 使 gem 的创建和维护变得非常简单。
关于继承,看看Ruby mixins如何用于封装和重用代码
跨越不同的阶级层次。 http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html
I know this question is quite old, but will hopefully be of use to those who come after...
I agree with what's been said above: gems are a great way to organize and reuse code.
In addition to the links above, may I recommend using Bundler to create gems as outlined
in this Ryan Bates RailsCast: http://railscasts.com/episodes/245-new-gem-with-bundler
I find that Bundler makes gem creation and maintenance very straightforward.
Regarding inheritance, check out how Ruby mixins can be used to encapsulate and reuse code
across disparate class hierarchies. http://ruby-doc.org/docs/ProgrammingRuby/html/tut_modules.html