New ruby project, with Git, RVM, Bundler and Ruby-Debug

November 14, 2010

Recently I’ve got into a nice groove using these tools on new ruby projects. I think of the following as a Koan, which I’ve practiced quite alot recently and which gives a nice environment to start working in.

The following is the a reasonably verbose version of this Koan.

Getting Started

Our new project will be called np.

➥  mkdir np
➥  cd np
[andy@airy-un ~/Sites/np] 

note: I have a two line bash prompt generally I’ll leave the first line out (the one without ➥) but not always, as sometimes it helps illustrate where we are.

Initialise Git repo, create .gitignore and make first commit

➥  git init
Initialized empty Git repository in /Users/andy/Sites/np/.git/
[andy@airy-un ~/Sites/np] (master) 
➥  vi .gitignore

  .bundle/
  .rvmrc

note: Here I am calling the editor vi, and showing what the file I’m editing looks like after I’ve finished editing and saving. The contents of the file will be useful when we add rvm and bundler

➥  ls -la
total 8
drwxr-xr-x   4 andy  staff   136 14 Nov 11:06 ./
drwxr-xr-x  45 andy  staff  1530 14 Nov 11:05 ../
drwxr-xr-x  12 andy  staff   408 14 Nov 11:07 .git/
-rw-r--r--   1 andy  staff    16 14 Nov 11:06 .gitignore

note: Just confirming that I’ve created a .gitignore file.

➥  git add .
➥  git commit -m ".gitignore"
[master (root-commit) 0250e5f] .gitignore
 1 files changed, 2 insertions(+), 0 deletions(-)
 create mode 100644 .gitignore
➥  git log
commit 0250e5f0996cc90702fcbf891d2410dd22f76d2a
Author: Andrew Premdas <apremdas@yahoo.co.uk>
Date:   Sun Nov 14 11:07:00 2010 +0000

   .gitignore

note: I’ve used long git commands in this post, but I have aliases for most common git commands e.g.

  • gst -> git status
  • ga -> git add
  • gch -> git checkout.

Setup RVM

➥  git checkout -b rvm
Switched to a new branch 'rvm'
[andy@airy-un ~/Sites/np] (rvm) 
➥  echo "rvm --create use 1.8.7@np > /dev/null" > .rvmrc

note: I’m using echo here so you can avoid having to fire up an editor if you are following this.

➥  cp .rvmrc .rvmrc.sample .

note: Here I am considering other users of our project. I’ve excluded the .rvmrc configuration file from the repository, so that different users can have their own configurations without breaking mine. The sample file is added to document the usage of .rvmrc.

[andy@airy-un ~/Sites/np] (rvm) 
➥  git add .
➥  git commit -m "sample config for rvm"
[rvm 662b0bc] sample config for rvm
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 .rvmrc.sample

note: We need to cd out and back into the folder to trigger the .rvmrc

➥  cd ..
[andy@airy-un ~/Sites] 
➥  cd np

  ============================================================
  RVM has encountered a not yet trusted .rvmrc file in the
  current working directory which contains the following code:
  ============================================================

rvm --create use 1.8.7@np > /dev/null

  ============================================================
  Trusting an .rvmrc file means that whenever you cd into the
  directory RVM will excecute this .rvmrc script in your shell

  Do you wish to trust this .rvmrc from now on?
  ============================================================
  (y for yes, n for no) > y

[andy@airy-un ~/Sites/np] (rvm) 

note: Now we can merge the rvm branch back into master, or we could start another branch right here. Git provides you with a multitude of choices and often there are several valid options. This is a bit of a problem for this article. Anyhow I’ll choose a simple approach here …

➥  git checkout master
Switched to branch 'master'
➥  git merge rvm
Updating 0250e5f..662b0bc
Fast-forward
 .rvmrc.sample |    1 +
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 .rvmrc.sample
➥  git log
commit 662b0bc56a4726f207a11c25adf7a25e09292977
Author: Andrew Premdas <apremdas@yahoo.co.uk>
Date:   Sun Nov 14 11:19:50 2010 +0000

    sample config for rvm

commit 0250e5f0996cc90702fcbf891d2410dd22f76d2a
Author: Andrew Premdas <apremdas@yahoo.co.uk>
Date:   Sun Nov 14 11:07:00 2010 +0000

    .gitignore

Setting up bundler

[andy@airy-un ~/Sites/np] (master) 
➥  git checkout -b bundler
Switched to a new branch 'bundler'
[andy@airy-un ~/Sites/np] (bundler) 
➥  vi Gemfile

source "http://rubygems.org"

# Ruby Debug
  gem "ruby-debug"     # 1.8.7
  # gem "ruby-debug19" # need this instead for 1.9.x

note: as ruby-debug needs a different gem for different rubies, I’ll document this in the Gemfile in addition to loading the gem

➥  gem install bundler
Successfully installed bundler-1.0.5
1 gem installed
Installing RDoc documentation for bundler-1.0.5...

note: I install bundler manually for each gemset I create, this is to ensure I keep upto date with bundler, but don’t break the environment of other projects if bundler somehow breaks backwards compatibility

➥  bundle install
Fetching source index for http://rubygems.org/
Installing columnize (0.3.2) 
Installing linecache (0.43) with native extensions 
Installing ruby-debug-base (0.10.4) with native extensions 
Installing ruby-debug (0.10.4) 
Using bundler (1.0.5) 

➥  git add .
[andy@airy-un ~/Sites/np] (bundler) 
➥  gca -m "added bundler and ruby-debug gem"
[bundler 0033b32] added bundler and ruby-debug gem
 2 files changed, 23 insertions(+), 0 deletions(-)
 create mode 100644 Gemfile
 create mode 100644 Gemfile.lock

note: Now we will merge back again and have a look at our log to see what we’ve done

➥  gch master
Switched to branch 'master'
➥  git merge bundler
Updating 662b0bc..0033b32
Fast-forward
 Gemfile      |    7 +++++++
 Gemfile.lock |   16 ++++++++++++++++
 2 files changed, 23 insertions(+), 0 deletions(-)
 create mode 100644 Gemfile
 create mode 100644 Gemfile.lock
[andy@airy-un ~/Sites/np] (master) 
➥  git log
commit 0033b32c487bc2fe3d91a6bb5c0bd6eb3ac24684
Author: Andrew Premdas <apremdas@yahoo.co.uk>
Date:   Sun Nov 14 11:42:05 2010 +0000

    added bundler and ruby-debug gem

commit 662b0bc56a4726f207a11c25adf7a25e09292977
Author: Andrew Premdas <apremdas@yahoo.co.uk>
Date:   Sun Nov 14 11:19:50 2010 +0000

    sample config for rvm

commit 0250e5f0996cc90702fcbf891d2410dd22f76d2a
Author: Andrew Premdas <apremdas@yahoo.co.uk>
Date:   Sun Nov 14 11:07:00 2010 +0000

    .gitignore

Configure Ruby Debug

note: I’ll skip the git branch stuff for this one, and use my aliases.

[andy@airy-un ~/Sites/np] (master) 
➥  vi .rdebugrc

set autoeval on
set history save on
set listsize 12
set autolist on

~
➥  ga .
[andy@airy-un ~/Sites/np] (master) 
➥  gc -m "nice env for debug sessions"
[master aab8f07] nice env for debug sessions
 1 files changed, 5 insertions(+), 0 deletions(-)
 create mode 100644 .rdebugrc
[andy@airy-un ~/Sites/np] (master) 
➥  git log
commit aab8f0729d6c16b09a738310d8507b00f5babafd
Author: Andrew Premdas <apremdas@yahoo.co.uk>
Date:   Sun Nov 14 11:47:32 2010 +0000

    nice env for debug sessions

commit 0033b32c487bc2fe3d91a6bb5c0bd6eb3ac24684
Author: Andrew Premdas <apremdas@yahoo.co.uk>
Date:   Sun Nov 14 11:42:05 2010 +0000

    added bundler and ruby-debug gem

commit 662b0bc56a4726f207a11c25adf7a25e09292977
Author: Andrew Premdas <apremdas@yahoo.co.uk>
Date:   Sun Nov 14 11:19:50 2010 +0000

    sample config for rvm

commit 0250e5f0996cc90702fcbf891d2410dd22f76d2a
Author: Andrew Premdas <apremdas@yahoo.co.uk>
Date:   Sun Nov 14 11:07:00 2010 +0000

    .gitignore

What have we achieved?

We have setup a uncontaminated ruby environment, setup a nice environment for debugging, setup a mechanism to manage our projects gem dependencies, and setup source control. All of this is nicely documented in our source control, and each piece of work is in a separate small commit so all can see how this was done.

To finish this post I just did all of the above on a new project. It took 6 minutes, including an interminable wait for rubygems.org to return its index when doing a bundle install. I’m convinced the benefits outweigh the cost