Oh wait, I meant to assign you to something

While doing some exploratory surgery via script/console the other day, I got clued into a neat little feature of irb: the underscore.  In short, it’s a special variable tied to the previous result.

>> Team.find(:all, :conditions => 'games_played > 500')
=> # [...a bunch of teams here...]

Oops.  I need to do a couple of operations on this set of teams.  Instead of retyping the above or going up/back in my irb history and changing the start of the line, I’ll just use the underscore. 

>> teams = _

Now, I can do what I originally wanted. 

>> winners = teams.select { |team| team.winning_percentage > 0.80 }.map(&:name)
>> losers = teams.select { |team| team.winning_percentage < 0.20 }.map(&:name)

Handy.

 

 

Posted in ruby. Tags: , . 2 Comments »

2 Responses to “Oh wait, I meant to assign you to something”

  1. Pat Nakajima Says:

    Ah yes. That’s a feature I often miss while just mucking about in irb.

  2. Andrew Grimm Says:

    @Pat It works for me in ruby 1.8 irb without rails.


Leave a Reply