Upgrading the script/console in Windows VISTA environment

If you haven’t been actively using script/console – you should start it now. It allows you to test things out in the environment of your project with a direct feedback. So, if you want to check those quirky database queries or how a new plugin works, run the script/console and start coding.

However, as seemingly everything in Ruby Rails, the console is a bit buggy in Windows. This calls for a console upgrade. At the same time we will include some further improvements as well to make your console the best firend.

1. Buggy cursor. Try it for yourself – type a very long line of code and try to move cursor – chances are it will get screwed up at some point in time – the cursor will jump, etc. The problems is with a module called readline. It’s a part of Ruby and was not updated from 2005, and nobody bothered to fix it for Windows. So, the only option is simply to disable it in your irb.bat file by adding the –noreadline switch like that:

@ECHO OFF
IF NOT "%~f0" == "~f0" GOTO :WinNT
@"C:\Ruby\bin\ruby.exe" "C:\Ruby\bin\irb" --noreadline %1 %2 %3 %4 %5 %6 %7 %8 %9
GOTO :EOF
:WinNT
"%~d0%~p0ruby.exe" "%~d0%~p0%~n0" --noreadline %*

Now, you won’t be able to use auto complete features of your console (tab), but the cursor works and the auto complete was not that useful for me.

While there, create a shortcut file called rc.bat in the ruby/bin directory, and insert there:

ruby script/console

2. Install HIRB and Wirble. HIRB is a nifty gem returns ActiveRecord objects in an SQL like table, which makes it much more readable:

irb>> Tag.last
  +-----+-------------------------+-------------+---------------+-----------+-----------+-------+
  | id  | created_at              | description | name          | namespace | predicate | value |
  +-----+-------------------------+-------------+---------------+-----------+-----------+-------+
  | 907 | 2009-03-06 21:10:41 UTC |             | gem:tags=yaml | gem       | tags      | yaml  |
  +-----+-------------------------+-------------+---------------+-----------+-----------+-------+
  1 row in set

To install hirb run: gem install hirb

Wirble does some colorizing stuff, although I have to say the default color scheme looks awful. To install wirble run: gem install wirble

You will also need to install win32console to get ANSI color support: gem install win32console

3. Now, automatically add HIRB (tabular query results), Wirble (color) and showing resulting SQL queries to the console.

In C:\Users\[your user name] create a file called “.irbrc” and paste these instructions there:

#
# allaboutruby.wordpress.com script/console configuration
# v 1.0, 2009 Nov

  require 'rubygems'

# load hirb
  require 'hirb'
  extend Hirb::Console
  Hirb::View.enable

# load Win32Console
  require 'Win32/Console/ANSI'

# start wirble (with color)
  require 'wirble'
  Wirble.init
  Wirble.colorize

# show SQL statements on in console mode
  if ENV.include?('RAILS_ENV') && !Object.const_defined?('RAILS_DEFAULT_LOGGER')
    require 'logger'
    RAILS_DEFAULT_LOGGER = Logger.new($stdout)
  end

So, now you have a modified console that works much better than the out of the box solution.