Starting a Ruby project

From Smith family

These are some steps to go though when starting a new Ruby project.

Create using Jeweler for new gems

  • If you want to turn your project into a gem, use Jeweler to set it up:
user@desktop:~$ jeweler --rspec my-new-project
user@desktop:~$ cd my-new-project
  • Create the version number for release:
user@desktop:~/my-new-project$ rake version:write MAJOR=0 MINOR=1 PATCH=0

You then need to modify a few files.

  • Gemfile: update the rspec line to use RSpec 2.6.x and use RDoc:
group :development do
  gem "rspec", "~> 2.6.0"
  gem "bundler", "~> 1.0.0"
  gem "jeweler", "~> 1.6.2"
  gem "rcov", ">= 0"
  gem "rdoc"
end
  • Rakefile: update it to use the new version of RDoc (thanks to Hints and Kinks). Change the two lines:
require 'rake/rdoctask'
Rake::RDocTask.new(:rdoc) do |rdoc|
to be
require 'rdoc/task'
RDoc::Task.new do |rdoc|
  • Rakefile: update the gem description in the gem.summary and gem.description lines.
  • gitignore: Make sure it doesn't pick up your editor temp files. Follow the instructions in the default file.

Set up git

Follow the standard instructions for setting up a new repository in Gitosis and Gitweb.

To echo it to Github as well, do:

user@desktop:~/my-new-project$ git remote add origin git@git.domain.tld:my-new-project.git
user@desktop:~/my-new-project$ git remote add github git@github.com:GithubUser/my-new-project.git

(Jeweler uses Github as the origin. If you want to change that, use git remote rename origin github first.)

In use, use:

user@desktop:~/my-new-project$ git commit -a -m 'Informative message'
user@desktop:~/my-new-project$ git push -u origin master
user@desktop:~/my-new-project$ git push -u github master

to commit and push the repository.

Create directory structure

Ruby files go in my-new-project/lib/module-name/, RSpec files got in my-new-project/spec/module-name. Create my-new-project/lib/module-name.rb to load all the files:

require 'module-name/file1'
require 'module-name/file2'
require 'module-name/file3'

Set up RSpec

  • Create my-new-project/.rspec to hold common options:
--color
  • Create my-new-project/spec/spec_helper.rb
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'rspec'
require 'module-name'

# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}

RSpec.configure do |config|
 
end

Use of Rake

  • rake spec runs the tests.
  • rake rdoc creates documentation.