I want my console!

When I had my first contact with Rails I was astonished. One of the coolest features for me was console. Total control of live application or just live testing. What You want. It must have been similar feeling to operate this console. Total control!

Console, but slightly different
Image from SXC

But best yet to come. I’m writing in Ruby also other applications. Read other – no Rails. In those applications You can have own console, too. Think about it, interactive shell to play with all data objects in Your program. How easy becomes testing ideas (like what would be if I changed that)? And having own console is easier than it seems.

I have looked through Rails console code and it turned out it is only few lines of code. So, here You have recipe for generic console. How to write own console in less than 5 lines of code:

irb = RUBY_PLATFORM =~ /(:?mswin|mingw)/ ? 'irb.bat' : 'irb'

libs =  " -r irb/completion"
libs << " -r config/setup"
exec "#{irb} #{libs} --simple-prompt"

I have put in my application all config data and require into config/setup. After executing this file with Ruby interpreter You have interactive shell to play with Your application.

Join the Conversation

3 Comments

  1. Pingback: Michal Hantl
  2. I wish I had examined this earlier. Creating console for own application is so easy ;) Ruby has so many nice surprises :)

    I’d saved a lot of time, when debugging my apps

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.