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.
June 2, 2008 at 2:21 pm
Ah yes. That’s a feature I often miss while just mucking about in irb.
March 12, 2009 at 11:28 pm
@Pat It works for me in ruby 1.8 irb without rails.