Class: IRB::History

Inherits:
Object show all
Defined in:
lib/irb/ext/history.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(size = 16) ⇒ History

Returns a new instance of History.



63
64
65
66
# File 'lib/irb/ext/history.rb', line 63

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

Instance Method Details

#[](idx) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/irb/ext/history.rb', line 75

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

#inspectObject



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/irb/ext/history.rb', line 94

def inspect
  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



87
88
89
90
# File 'lib/irb/ext/history.rb', line 87

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

#real_inspectObject



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

alias real_inspect inspect

#size(size) ⇒ Object



68
69
70
71
72
73
# File 'lib/irb/ext/history.rb', line 68

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