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.