用于组织多文件 Ruby 程序的系统?

发布于 2024-08-16 18:17:50 字数 683 浏览 15 评论 0 原文

是否有组织多文件 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

開玄 2024-08-23 18:17:50

您可能需要考虑为您的图书馆创建一个 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.

从来不烧饼 2024-08-23 18:17:50

除了 vonconrad 的答案中推荐的链接外,您还可以参考 编程 Ruby 1.9,作者:Dave Thomas 等人。有一个本书该部分的免费示例 PDF

本章提到:

16.1 命名空间 我们已经遇到过 Ruby 帮助您管理程序中事物名称的方法。如果您定义方法或
类中的常量,Ruby 确保它们的名称只能使用
在该类的上下文中

16.2 组织源代码小型、独立的脚本可以位于单个文件中...较大的程序应考虑 RubyGems 系统

anagram/ <- top-level
  bin/ <- command-line interface goes here
  lib/ <- three library files go here
  test/ <- test files go here

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:

16.1 Namespaces We’ve already encountered a way that Ruby helps you manage the names of things in your programs. If you define methods or
constants in a class, Ruby ensures that their names can be used only
in the context of that class

16.2 Organizing Your Source Small, self-contained scripts can be in a single file... bigger programs should consider the RubyGems system

anagram/ <- top-level
  bin/ <- command-line interface goes here
  lib/ <- three library files go here
  test/ <- test files go here
椒妓 2024-08-23 18:17:50

我知道这个问题已经很老了,但希望对后来的人有用...

我同意上面所说的: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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文