我如何建立一个基本的 Ruby 项目? [英] How do I set up a basic Ruby project?

查看:71
本文介绍了我如何建立一个基本的 Ruby 项目?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个包含 10 ~ 20 个类/文件的小型 Rub​​y 项目.我需要一些 gem,我想使用 RSpec 作为测试框架.

I want to create a small Ruby project with 10 ~ 20 classes/files. I need some gems and I want to use RSpec as a test framework.

我以后可能想构建一个 gem,但这并不确定.

I might want to build a gem later on, but that is not certain.

是否有一些操作方法或指南可以向我展示如何设置项目的基本结构?

Is there some how-to or guide that shows me how to set up the basic structure of my project?

我的问题是:

  • 在哪里放置我的所有自定义错误/异常?
  • 是否有一些命名目录(如 lib、bin、src 等)的约定?
  • 在哪里放置测试数据或文档?
  • 我在哪里需要我的所有文件才能在我的项目中访问它们?

我知道我可以从头开始做任何事情,但我需要一些指导.有一些不错的宝石可以复制,但我不确定我真正需要什么以及可以删除什么.

I know I could do everything from scratch, but I would like some guidance. There are some good gems out there that I could copy, but I am not certain what I really need and what I can delete.

我查看了 http://gembundler.com/,但在设置 Bundler 后它停止了.>

I looked at http://gembundler.com/, but it stops after setting up Bundler.

推荐答案

要获得良好的开端,您可以使用 bundle gem 命令和 rspec --init.

To get a good start, you can use the bundle gem command and rspec --init.

~/code $ bundle gem my_lib
      create  my_lib/Gemfile
      create  my_lib/Rakefile
      create  my_lib/LICENSE.txt
      create  my_lib/README.md
      create  my_lib/.gitignore
      create  my_lib/my_lib.gemspec
      create  my_lib/lib/my_lib.rb
      create  my_lib/lib/my_lib/version.rb
Initializating git repo in /Users/john/code/my_lib
~/code $ cd my_lib/
~/code/my_lib $ git commit -m "Empty project"
~/code/my_lib $ rspec --init
The --configure option no longer needs any arguments, so true was ignored.
  create   spec/spec_helper.rb
  create   .rspec

  • 代码进入lib
  • 规范进入spec
  • 测试数据或文档在spec/fixtures/
  • 需要 lib/my_lib.rb 中的所有 ruby​​ 文件.您也可以根据自己的喜好在该文件中或在它们自己的文件中定义例外.
  • C 源文件放在 ext/my_lib
  • shell 脚本和可执行文件进入 bin
    • code goes in lib
    • specs go in spec
    • test data or documents go in spec/fixtures/
    • Require all your ruby files in lib/my_lib.rb. You can define your exceptions in that file, too, or in their own files -- according to your own preference.
    • C source files go in ext/my_lib
    • shell scripts and executables go in bin
    • 如有疑问,只需看看其他宝石的布局.

      When in doubt, just look at how other gems are laid out.

      更多信息:

      您应该在 gemspec 中添加 rspec 作为开发依赖项,以便其他开发人员更轻松

      You should add rspec as a development dependency in your gemspec to make things easier for other developers

      1. 编辑 my_lib.gemspec,在底部附近添加 gem.add_development_dependency 'rspec'gem.add_development_dependency 'rake'.
      2. Bundler.setuprequire 'my_lib' 添加到 spec/spec_helper.rb 的顶部,以确保在运行规范时加载 gem 依赖项.立>
      3. 添加 require "rspec/core/rake_task"task :default =>:spec 到您的 Rakefile,以便运行 rake 将运行您的规范.
      1. Edit my_lib.gemspec, adding gem.add_development_dependency 'rspec' and gem.add_development_dependency 'rake' near the bottom.
      2. Add Bundler.setup and require 'my_lib' to the top of spec/spec_helper.rb to ensure your gem dependencies are loaded when you run your specs.
      3. Add require "rspec/core/rake_task" and task :default => :spec to your Rakefile, so that running rake will run your specs.

      在您进行最新创作时,guard-rspec 可以节省您的时间并在文件更改时自动运行您的规范,提醒您规范失败.

      While you're working on your newest creation, guard-rspec can save you time and hassle by automatically running your specs as files change, alerting you to spec failures.

      ~/code/my_lib $ git add spec/spec_helper.rb
      ~/code/my_lib $ git commit -am "Add RSpec"
      ~/code/my_lib $ vim my_lib.gemspec # add guard development dependency
      ~/code/my_lib $ bundle
      ~/code/my_lib $ bundle exec guard init
      ~/code/my_lib $ vim Guardfile # Remove the sections below the top one
      ~/code/my_lib $ git add Guardfile
      ~/code/my_lib $ git commit -am "Add Guard"
      

      对自己的创作感到满意后,将其推送到 github

      After you're happy with your creation, push it up to github

      # create a github repository for your gem, then push it up
      ~/code/my_lib $ curl -u myusername https://api.github.com/user/repos -d '{"name":"my_lib"}' 
      ~/code/my_lib $ git remote add origin git@github.com:myusername/my_lib.git
      ~/code/my_lib $ git push
      

      然后,当您准备好在 Rubygems.org 上发布您的 gem 时,运行 rake release,它将引导您完成这些步骤.

      Then, when you're ready to release your gem on Rubygems.org, run rake release, which will walk you through the steps.

      ~/code/my_lib $ rake release
      

      进一步参考

      • The Rubygems patterns guide (and home page), from Matheus Moreira's answer. They're really great references
      • How I Start by Steve Klabnik
      • Exercise 46: A Project Skeleton from Zed Shaw's Learn Ruby The Hard Way
      • New Gem with Bundler video on Railscasts
      • docs

      这篇关于我如何建立一个基本的 Ruby 项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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