Tim Riley

Adaptive script/console Shell Alias for both Rails and Sinatra

March 6th 2009

Like many keystroke-efficient Rails hackers, I’ve long had a line in my .bash_profile file to alias sc to script/console, along with a host of other tricks.

This shortcut was more than sufficient until recently, when I started writing Sinatra apps. The minimal framework that it is, Sinatra doesn’t provide a console script like Rails, but I found you can easily achieve the same effect by running irb -r your_sinatra_app.rb.

Not wanting my fingers to have to deviate from habits long held, I changed my sc alias into a full-blown bash function that will drop you into a Rails console, Sinatra console or just a plain irb console based on your location within the filesystem:

function sc {
  if [ -x script/console ]; then
    script/console
  else
    sinatra_rb=`egrep -l "^require.+sinatra.$" *.rb 2>/dev/null`
    if [ -e $sinatra_rb ]; then
      irb -r $sinatra_rb
    else
      irb
    fi
  fi
}

Throw it in your .bash_profile and have fun!

Tim Riley

Hi, I'm Tim Riley. I'm a web developer based in Canberra, Australia, where I work at a small design studio called Icelab. This is my blog, where I can share with you the things I learn as I go about building products for the web.

What I Build

Recent Presentations

  1. Facebook Authentication with OAuth
  2. JavaScript Testing with Capybara and Cucumber
  3. Using Titanium to Build Mobile Apps with JavaScript (audio)
  4. An Introduction to Sinatra
  5. Building your Capistrano Recipe Book