Jay McGavren's Journal

2009-08-26

Quick HOWTO for Hirb formatter for irb/Rails console...

Hirb is pretty object formatting for irb (and the Rails console). If squinting at the standard Object#inspect output is slowing you down (trust me, it is), you should look into Hirb. Here’s <5 minute setup…

Install the gem:

sudo gem install hirb

Add these lines to your $HOME/.irbrc file, which will cause Hirb to be auto-loaded at startup for both irb and script/console:

require 'rubygems'

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

Try it out in irb:

$ irb
irb(main):001:0> table ['foo', 'bar']
+-------+
| value |
+-------+
| foo   |
| bar   |
+-------+
2 rows in set
=> true
irb(main):002:0>

Try it out in script/console:

jay@dandelion:~/Projects/myproject
$ script/console
Loading development environment (Rails 2.3.2)
>> MyModel.all
+----+-----------------+-------------------------+-------------------------+
| id | name            | created_at              | updated_at              |
+----+-----------------+-------------------------+-------------------------+
| 1  | Foo             | 2009-06-16 23:38:10 UTC | 2009-06-16 23:38:10 UTC |
| 2  | Bar             | 2009-06-17 00:38:16 UTC | 2009-06-17 00:38:16 UTC |
+----+-----------------+-------------------------+-------------------------+
2 rows in set

Hirb has customization options I haven’t even begun to explore, so be sure to look at its docs.

comments powered by Disqus