Class: IRB::History

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

Instance Method Summary collapse

Constructor Details

#initialize(size = 16) ⇒ History

Returns a new instance of History.



53
54
55
56
# File 'lib/irb/ext/history.rb', line 53

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

Instance Method Details

#[](idx) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
# File 'lib/irb/ext/history.rb', line 65

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

#inspectObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/irb/ext/history.rb', line 84

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



77
78
79
80
# File 'lib/irb/ext/history.rb', line 77

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

#real_inspectObject



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

alias real_inspect inspect

#size(size) ⇒ Object



58
59
60
61
62
63
# File 'lib/irb/ext/history.rb', line 58

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