Class: Pry::HistoryArray
- Includes:
- Enumerable
- Defined in:
- lib/pry/history_array.rb
Overview
A history array is an array to which you can only add elements. Older entries are removed progressively, so that the array never contains more than N elements.
History arrays are used by Pry to store the output of the last commands.
Instance Attribute Summary collapse
-
#max_size ⇒ Integer
readonly
Maximum amount of objects in the array.
Instance Method Summary collapse
-
#<<(value) ⇒ Object
Pushes an object at the end of the array.
- #[](index_or_range, size = nil) ⇒ Object
- #each ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(size) ⇒ HistoryArray
constructor
A new instance of HistoryArray.
- #inspect ⇒ Object
- #pop! ⇒ Object
-
#size ⇒ Integer
(also: #count, #length)
Amount of objects in the array.
- #to_a ⇒ Object
-
#to_h ⇒ Object
Returns [Hash] copy of the internal @hash history.
Constructor Details
#initialize(size) ⇒ HistoryArray
Returns a new instance of HistoryArray.
20 21 22 23 24 25 |
# File 'lib/pry/history_array.rb', line 20 def initialize(size) @max_size = size @hash = {} @count = 0 end |
Instance Attribute Details
#max_size ⇒ Integer (readonly)
Returns Maximum amount of objects in the array.
107 108 109 |
# File 'lib/pry/history_array.rb', line 107 def max_size @max_size end |
Instance Method Details
#<<(value) ⇒ Object
Pushes an object at the end of the array
29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/pry/history_array.rb', line 29 def <<(value) @hash[@count] = value if @hash.size > max_size @hash.delete(@count - max_size) end @count += 1 self end |
#[](index) ⇒ Object? #[](index, size) ⇒ Array? #[](range) ⇒ Array?
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/pry/history_array.rb', line 53 def [](index_or_range, size = nil) if index_or_range.is_a? Integer index = convert_index(index_or_range) if size end_index = index + size index > @count ? nil : (index...[end_index, @count].min).map do |n| @hash[n] end else @hash[index] end else range = convert_range(index_or_range) range.begin > @count ? nil : range.map { |n| @hash[n] } end end |
#each ⇒ Object
82 83 84 85 86 |
# File 'lib/pry/history_array.rb', line 82 def each ((@count - size)...@count).each do |n| yield @hash[n] end end |
#empty? ⇒ Boolean
78 79 80 |
# File 'lib/pry/history_array.rb', line 78 def empty? size == 0 end |
#inspect ⇒ Object
102 103 104 |
# File 'lib/pry/history_array.rb', line 102 def inspect "#<#{self.class} size=#{size} first=#{@count - size} max_size=#{max_size}>" end |
#pop! ⇒ Object
97 98 99 100 |
# File 'lib/pry/history_array.rb', line 97 def pop! @hash.delete @count - 1 @count -= 1 end |
#size ⇒ Integer Also known as: count, length
Returns Amount of objects in the array.
72 73 74 |
# File 'lib/pry/history_array.rb', line 72 def size @count end |
#to_a ⇒ Object
88 89 90 |
# File 'lib/pry/history_array.rb', line 88 def to_a ((@count - size)...@count).map { |n| @hash[n] } end |
#to_h ⇒ Object
Returns [Hash] copy of the internal @hash history
93 94 95 |
# File 'lib/pry/history_array.rb', line 93 def to_h @hash.dup end |