January 21, 2010

Console tip: retrieve the last return value with underscore

rubyquicktips:

In IRB you can retrieve the last return value from a command by using the underscore _ sign:

$ irb
>> 2*3
=> 6
>> _ + 7
=> 13
>> _
=> 13

This also works in the Rails console:

$ script/console
>> User.first
=> #<User id: 7, first_name ...
>> user = _
=> #<User id: 7, first_name ...
>> user
=> #<User id: 7, first_name ...

This is quite useful, when you forgot to assign the return value of an expression to a variable.

  1. noplans reblogged this from rubyquicktips
  2. yhara reblogged this from rubyquicktips
  3. aognevsky reblogged this from rubyquicktips
  4. layer13 reblogged this from rubyquicktips
  5. mattonrails reblogged this from rubyquicktips
  6. rubyquicktips posted this