Script/console tip for the lazy 12

Posted by jeff Thursday, July 26, 2007 15:19:00 GMT

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

Comments

Leave a response

  1. Terry   July 26, 2007 @ 04:57 PM

    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.

    a = ["foo", "bar", "baz"] a[0] 'foo' _ 'foo' _.upper() 'FOO'

    Laziness rules.

  2. Shalev   July 26, 2007 @ 06:42 PM

    To save even a few more keystrokes, you can do:

    _.collect { &:title }

    or even

    _.map{&:title}

  3. divemountain   July 26, 2007 @ 06:54 PM

    '_' is handy indeed. I also like

    require 'pp'

    so you can 'pretty print' hashes pp products

  4. Dr J   July 26, 2007 @ 08:23 PM

    Nice find. Wish I had known about this a while ago

  5. Chris   July 26, 2007 @ 11:39 PM

    Hey I thought you read my article!

    http://www.softiesonrails.com/2006/9/8/stuff-i-and-maybe-you-didn-t-know-about-irb

  6. Chris W   July 26, 2007 @ 11:47 PM

    @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.

  7. fallenrogue   July 27, 2007 @ 12:59 AM

    fantastic tip!

  8. Brian Eng   July 27, 2007 @ 02:21 AM

    Chris: Uh, yeah. We just have really poor memories.

  9. Tim Lucas   July 27, 2007 @ 08:41 AM

    Correction to Shalev: .collect &:title or .collect(&:title)

  10. ddf   February 12, 2008 @ 05:15 AM

    gfgf

  11. kino   May 24, 2008 @ 01:11 AM

    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.

  12. Ellroy   May 30, 2008 @ 11:18 PM

    We can be sure that cogitationes, if we maintain this attitude, are precisely what make critical decisions about the phenomenological Ego at all possible.

Comment


(won't be published)