Ruby Quicktips Logo

Ruby Quicktips

Random Ruby and Rails tips.
This blog is dedicated to deliver short, interesting and practical tidbits of the Ruby language and Ruby on Rails framework. Read more...

Your submissions are more than welcome!
Jan 19 ’10

Console tip: retrieve the last return value with underscore

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.

24 notes 0 comments

  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. rubyquicktips posted this

Comments

You can use HTML tags for formatting. Wrap code in <code> tags and multiple lines of code in <pre><code> tags.

blog comments powered by Disqus