#protip - rails console
So for anyone who doesn’t know, the Rails console (rails c in Rails3 or
script/console in Rails2) runs up an irb process behind the scenes. It
also loads Rails into the context, and a few other bits – but mainly it’s an
irb session.
I’ve just been getting a couple of “Oh wow, I didn’t know that.” responses
when I mention these things in #RubyOnRails, so I thought I’d just post
this here.
Thing 1: the underscore method
The return from every command that you enter into irb is available in the
underscore method _. So, if you’re like me, you often run a command and
then think that you should have assigned it to some variable, or
pretty-printed it or something.
The underscore method is perfect for this:
>> City.all => [#<City id: 218996838, name: "Chatswood", postcode: "2067", state_id: 944944227, created_at: "2009-03-30 01:56:22", updated_at: "2009-03-30 01:56:22">, #<City id: 957448139, name: "Roseville", postcode: "2069", state_id: 944944227, created_at: "2009-03-30 01:56:22", updated_at: "2009-03-30 01:56:22">, #<City id: 1015505586, name: "Willoughby", postcode: "2068", state_id: 944944227, created_at: "2009-03-30 01:56:22", updated_at: "2009-03-30 01:56:22">]
Bugger me if I didn’t just forget to assign that to something…
>> c = _ => [#<City id: 218996838, name: "Chatswood", postcode: "2067", state_id: 944944227, created_at: "2009-03-30 01:56:22", updated_at: "2009-03-30 01:56:22">, #<City id: 957448139, name: "Roseville", postcode: "2069", state_id: 944944227, created_at: "2009-03-30 01:56:22", updated_at: "2009-03-30 01:56:22">, #<City id: 1015505586, name: "Willoughby", postcode: "2068", state_id: 944944227, created_at: "2009-03-30 01:56:22", updated_at: "2009-03-30 01:56:22">]
Yay!
The only thing to remember is that if you don’t grab it immediately, then it will be gone next command – it is overwritten with every line.
Thing 2: the y method
This is actually just the Kernel#y method, but it’s particularly useful in
irb sessions when you want to print something out in an easy to read (and
easy to grab if you want YAML) format.
>> { :foo => 'bar', :woo => { :ooo => 1 } }
=> {:foo=>"bar", :woo=>{:ooo=>1}}
>> y _
---
:foo: bar
:woo:
:ooo: 1
=> nilThis is especially good for outputting large collections of Rails models.
Thing 3: the pp library
The pp or Pretty-Print lib is also great for printing out collections of
data in a neat way. Just require it into your irb session and use it to
great effect:
>> require 'pp' => true >> pp City.all [#<City id: 218996838, name: "Chatswood", postcode: "2067", state_id: 944944227, created_at: "2009-03-30 01:56:22", updated_at: "2009-03-30 01:56:22">, #<City id: 957448139, name: "Roseville", postcode: "2069", state_id: 944944227, created_at: "2009-03-30 01:56:22", updated_at: "2009-03-30 01:56:22">, #<City id: 1015505586, name: "Willoughby", postcode: "2068", state_id: 944944227, created_at: "2009-03-30 01:56:22", updated_at: "2009-03-30 01:56:22">] => nil
Voila! To make that even easier, see the next thing.
Thing 4: .irbrc
By default in *nix based operating systems the file .irbrc in your $HOME
directory will be loaded by irb when it starts up. (On Windows you need
to create your own irbrc anywhere you like and set the IRBRC environment
variable).
You can put any Ruby commands you like in here, here’s mine:
require 'rubygems' require 'irb/completion' require 'pp' require 'yaml' def v(x) IO.popen( 'mvim -', 'w') do |io| io.puts x.to_yaml end end def less(x) IO.popen( 'less -', 'w') do |io| io.puts x.to_yaml end end
You can see that it’s simple to require in your choice of libs in there, and you can even define your own methods, which brings us to the last thing.
Thing 5: total customization
The .irbrc is just Ruby, and as you’ve seen above you can define your own
methods. I’ve defined two to make it easy to pipe an object (as yaml) out
to an external program (I’ve got mvim and less setup) – but you can have
anything you like. This lets you customize your own irb interface with
whatever little helpers you want to make your work easier.
Take a look here and here at some powerful examples of what can be done.
Enjoy.