Reason #135 • May 15th, 2026

Getting any previous value in IRB with __

Most developers who have dabbled with IRB for a bit have probably figured out the _ variable discussed yesterday.

A lesser-known feature is that you can set the eval_history IRB config to enable the history object, assigned to the __ variable. This allows you to access not just the last result but any previous result from the current IRB session.

NOTE: The following example cannot be copy-pasted into IRB as is, because the __ variable is only populated after each expression is evaluated. To try it out, copy and paste each line one by one.

Ruby
conf.eval_history = 100 # Keep the last 100 results in history

1 + 2 # => 3
3 * 4 # => 12
__[2] + __[3] # => 15

__
# Output:
# 1 100
# 2 3
# 3 12
# 4 15
    

This is admittedly a niche feature, but every once in a while I like to surface features that even seasoned Rubyists likely aren't aware of 😊

If you'd like to enable this feature by default, you can add IRB.conf[:EVAL_HISTORY] = 100 to your ~/.irbrc file.

History

The __ history variable shipped in Ruby 1.8.0, released in August 2003.

Reason #136 ?