理想的 Ruby 项目结构

发布于 2024-07-14 17:57:20 字数 389 浏览 5 评论 0原文

我正在概述/澄清 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 技术交流群。

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

发布评论

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

评论(4

香橙ぽ 2024-07-21 17:57:21

我认为这非常正确。 默认情况下,Rubygems 会将 lib 目录添加到加载路径,但您可以使用 $: 变量将任何您想要的目录推送到该目录上。 也就是说

$:.push File.expand_path(File.dirname(__FILE__) + '/../surfcompstuff')

,当您在该目录中添加 surfer.rb 时,您可以在任何地方 require "surfer" 并且会找到该文件。

另外,作为惯例,类和单例获取文件,模块获取目录。 例如,如果您有 LolCatz 模块和 LolCatz::Moar 类,则如下所示:

lib/
  appname.rb
  lolcatz/
    moar.rb

这就是为什么有一个 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.

$:.push File.expand_path(File.dirname(__FILE__) + '/../surfcompstuff')

That means when you have say, surfer.rb in that dir, you can require "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 the LolCatz::Moar class that would look like:

lib/
  appname.rb
  lolcatz/
    moar.rb

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.

猫腻 2024-07-21 17:57:21

请参阅 http://guides.rubygems.org/what-is-a 中的以下示例-宝石/

 % tree freewill
    freewill/
    ├── bin/
    │   └── freewill
    ├── lib/
    │   └── freewill.rb
    ├── test/
    │   └── test_freewill.rb
    ├── README
    ├── Rakefile
    └── freewill.gemspec

See the following example from http://guides.rubygems.org/what-is-a-gem/

 % tree freewill
    freewill/
    ├── bin/
    │   └── freewill
    ├── lib/
    │   └── freewill.rb
    ├── test/
    │   └── test_freewill.rb
    ├── README
    ├── Rakefile
    └── freewill.gemspec
余生再见 2024-07-21 17:57:21

我尝试模仿 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.

你げ笑在眉眼 2024-07-21 17:57:21

如果您使用捆绑器,运行此命令 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)

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