理想的 Ruby 项目结构 [英] Ideal Ruby project structure

查看:59
本文介绍了理想的 Ruby 项目结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对 ruby​​(非 Rails/merb/etc)项目的理想项目结构进行概述/澄清.我猜它如下

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?

推荐答案

我认为这很正确.默认情况下,Rubygems 会将 lib 目录添加到加载路径,但您可以使用 $: 变量将您想要的任何目录推送到该目录中.即

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')

这意味着当您在该目录中说 surfer.rb 时,您可以 require "surfer" 任何地方,然后就会找到该文件.

That means when you have say, surfer.rb in that dir, you can require "surfer" anywhere and the file will be found.

此外,作为惯例,类和单例获得一个文件,模块获得一个目录.例如,如果你有 LolCatz 模块和 LolCatz::Moar 类,它看起来像:

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

这就是为什么有一个 lib/appname 文件夹的原因,因为大多数库都在 appname 命名空间中.

That is why there is an lib/appname folder because most libraries are in the appname namespace.

此外,如果您尝试运行命令 newgem --simple [projectname],它将快速为您生成一个脚手架,其中仅包含 Ruby 项目的基本要素(以及扩展为 Ruby Gem).我知道还有其他工具可以做到这一点,但 newgem 很常见.我通常会删除 TODO 文件和所有脚本内容.

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.

这篇关于理想的 Ruby 项目结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆