Class: IRB::History

Inherits:
Object show all
Defined in:
lib/irb/ext/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) ⇒ History

:nodoc:



100
101
102
103
# File 'lib/irb/ext/history.rb', line 100

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).



113
114
115
116
117
118
119
120
121
122
123
# File 'lib/irb/ext/history.rb', line 113

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:



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/irb/ext/history.rb', line 132

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:



125
126
127
128
# File 'lib/irb/ext/history.rb', line 125

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

#real_inspectObject



130
# File 'lib/irb/ext/history.rb', line 130

alias real_inspect inspect

#size(size) ⇒ Object

:nodoc:



105
106
107
108
109
110
# File 'lib/irb/ext/history.rb', line 105

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