123456789_123456789_123456789_123456789_123456789_

Class: IRB::EvalHistory

Relationships & Source Files
Inherits: Object
Defined in: lib/irb/ext/eval_history.rb

Overview

Represents history of results of previously evaluated commands.

Available via __ variable, only if IRB.conf[:EVAL_HISTORY] or IRB::CurrentContext().eval_history is non-nil integer value (by default it is nil).

Example (in irb):

# Initialize history
IRB::CurrentContext().eval_history = 10
# => 10

# Perform some commands...
1 + 2
# => 3
puts 'x'
# x
# => nil
raise RuntimeError
# ...error raised

# Inspect history (format is "<item number> <evaluated value>":
__
# => 1 10
# 2 3
# 3 nil

__[1]
# => 10

Class Method Summary

Instance Method Summary

Constructor Details

.new(size = 16) ⇒ EvalHistory

This method is for internal use only.
[ GitHub ]

  
# File 'lib/irb/ext/eval_history.rb', line 94

def initialize(size = 16)  # :nodoc:
  @size = size
  @contents = []
end

Instance Method Details

#[](idx)

Get one item of the content (both positive and negative indexes work).

[ GitHub ]

  
# File 'lib/irb/ext/eval_history.rb', line 107

def [](idx)
  begin
    if idx >= 0
      @contents.find{|no, val| no == idx}[1]
    else
      @contents[idx][1]
    end
  rescue NameError
    nil
  end
end

#inspect

This method is for internal use only.
[ GitHub ]

  
# File 'lib/irb/ext/eval_history.rb', line 126

def inspect  # :nodoc:
  if @contents.empty?
    return real_inspect
  end

  unless (last = @contents.pop)[1].equal?(self)
    @contents.push last
    last = nil
  end
  str = @contents.collect{|no, val|
    if val.equal?(self)
      "#{no} ...self-history..."
    else
      "#{no} #{val.inspect}"
    end
  }.join("\n")
  if str == ""
    str = "Empty."
  end
  @contents.push last if last
  str
end

#push(no, val)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/irb/ext/eval_history.rb', line 119

def push(no, val)  # :nodoc:
  @contents.push [no, val]
  @contents.shift if @size != 0 && @contents.size > @size
end

#real_inspect

[ GitHub ]

  
# File 'lib/irb/ext/eval_history.rb', line 124

alias real_inspect inspect

#size(size)

This method is for internal use only.
[ GitHub ]

  
# File 'lib/irb/ext/eval_history.rb', line 99

def size(size) # :nodoc:
  if size != 0 && size < @size
    @contents = @contents[@size - size .. @size]
  end
  @size = size
end