Script/console tip for the lazy 12
I use script/console a lot. It's a great way to learn about the Rails API, inspect objects, and get really good with ActiveRecord subtleties.
Yesterday I was doing this in script/console:
>> Product.find :all, :order => 'title asc'
=> [#<Product:0x342ee58 @attributes={"title"=>"Another", "id"=>"2", "released"=>nil, "version_number"=>"1.0"}>,
#<Product:0x342ee1c @attributes={"title"=>"My Product", "id"=>"1", "released"=>nil, "version_number"=>"1.5"}>]
Good, there's a couple of products in my development database to play with. I wanted to get all of the titles in an array, sorted in ascending order. So I need to take the results of my previous statement and call collect on it - so I was about to do this:
>> Product.find(:all, :order => 'title asc').collect { |product| product.title }
With irb history installed, I can just hit my up-arrow to recall the previous statement. But I'd still have to edit it, to insert the parentheses, before I could append the collect iteration code. So what does a lazy person do when confronted with a task that might take a second or two? Why, spend a lot more time googling for a better solution, of course.
Sure enough, after a few minutes with my good friend Google, I found it:
>> _.collect { |product| product.title }
Yes, that's an underscore character in front of the .collect. In irb (and hence, script/console), an underscore is a kind of global variable that holds the last result. So in my case, it represents the collection that I had just found. You can also capture it in a variable if you want:
>> products = _
>> products.size
=> 2
Just a tip for anyone out there that's just as lazy as me.
Ready to learn more? Sign up now for Essential Rails



Noticed a similar idiom in Python actually in David Goodear's Idiomatic Python presentation @ PyCon.
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html
[me@me] home :: python Python 2.4.4 (#1, Oct 18 2006, 10:34:39) [GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin Type "help", "copyright", "credits" or "license" for more information.
Laziness rules.
To save even a few more keystrokes, you can do:
_.collect { &:title }
or even
_.map{&:title}
'_' is handy indeed. I also like
require 'pp'
so you can 'pretty print' hashes pp products
Nice find. Wish I had known about this a while ago
Hey I thought you read my article!
http://www.softiesonrails.com/2006/9/8/stuff-i-and-maybe-you-didn-t-know-about-irb
@divemountain
you can also use:
y <hash> or <activerecord>
and get something like this:
:first_name: Chris :last_name: W
y converts the hash to yaml and displays it.
fantastic tip!
Chris: Uh, yeah. We just have really poor memories.
Correction to Shalev:
.collect &:titleor.collect(&:title)gfgf
By virtue of pure reason, the noumena are the clue to the discovery of, when thus treated as our knowledge, the paralogisms; thus, reason is just as necessary as, then, reason.
We can be sure that cogitationes, if we maintain this attitude, are precisely what make critical decisions about the phenomenological Ego at all possible.