Making irb behave like the Rails console

A project I’m working on using Ruby has a bunch of functions that are usually run in groups but from time to time will need to be run individually. My initial solution to this problem was to create a whole mess of shell scripts to run the individual pieces. However, that looked like it was going to require a lot of typing, and I don’t much like to type.

Faced with actual work, I remembered the Rails console. This struck me as the ideal solution. It does command completion, so I wouldn’t have to remember the specifics of every little command. It turns out that the console is nothing more than irb with a few flags. Specifically:

 irb -r irb/completion --simple-prompt

Given this, I created a file with shortcuts for the pieces of code that I would want to run:

 #
 # shortcuts.rb
 #
 require 'project/important_stuff'
 require 'project/other_stuff'

 def do_thing_a
    ...
 end

 def do_thing_b
    ...
 end

 def something_else
    ...
 end

 def whatever_more
    ...
 end

And created a shell script to wrap the whole thing up:

 #!/bin/sh
 #
 # console.sh
 #
 exec irb -r irb/completion -r shortcuts --simple-prompt "$@"

So now I can run “console.sh” and type things like “do<tab>a” or “wh<tab>” and have an easy way to run my code. This pleases me.

Leave a Reply


This is a free Wordpress template provided by Mathew Browne | Web Design | SEO