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.



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

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

Instance Method Details

#[](idx) ⇒ Object



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

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

#inspectObject Also known as: real_inspect



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

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



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

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

#size(size) ⇒ Object



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

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