Class: IRB::EvalHistory

Inherits:
Object show all
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

Instance Method Summary collapse

Constructor Details

#initialize(size = 16) ⇒ EvalHistory

:nodoc:



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

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

Instance Method Details

#[](idx) ⇒ Object

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



107
108
109
110
111
112
113
114
115
116
117
# 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

#inspectObject

:nodoc:



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# 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) ⇒ Object

:nodoc:



119
120
121
122
# 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_inspectObject



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

alias real_inspect inspect

#size(size) ⇒ Object

:nodoc:



99
100
101
102
103
104
# 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