Class: Lich::Common::LimitedArray
- Inherits:
-
Array
- Object
- Array
- Lich::Common::LimitedArray
- Defined in:
- documented/common/limitedarray.rb
Overview
Represents an array with a maximum size limit.
This class extends the standard Array to enforce a maximum size, automatically removing the oldest elements when the limit is reached.
Instance Attribute Summary collapse
-
#max_size ⇒ Object
Returns the value of attribute max_size.
Instance Method Summary collapse
-
#history ⇒ Array
Returns an empty array representing the history of elements.
-
#initialize(size = 0, obj = nil) ⇒ LimitedArray
constructor
Initializes a new LimitedArray instance.
-
#push(line) ⇒ Object
Adds an element to the LimitedArray, removing the oldest elements if the maximum size is exceeded.
-
#shove(line) ⇒ Object
private
Adds an element to the LimitedArray, similar to push.
Constructor Details
#initialize(size = 0, obj = nil) ⇒ LimitedArray
Initializes a new LimitedArray instance.
17 18 19 20 |
# File 'documented/common/limitedarray.rb', line 17 def initialize(size = 0, obj = nil) @max_size = 200 super end |
Instance Attribute Details
#max_size ⇒ Object
Returns the value of attribute max_size.
11 12 13 |
# File 'documented/common/limitedarray.rb', line 11 def max_size @max_size end |
Instance Method Details
#history ⇒ Array
Returns an empty array representing the history of elements.
40 41 42 |
# File 'documented/common/limitedarray.rb', line 40 def history Array.new end |
#push(line) ⇒ Object
Adds an element to the LimitedArray, removing the oldest elements if the maximum size is exceeded.
25 26 27 28 |
# File 'documented/common/limitedarray.rb', line 25 def push(line) self.shift while self.length >= @max_size super end |
#shove(line) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Adds an element to the LimitedArray, similar to push.
34 35 36 |
# File 'documented/common/limitedarray.rb', line 34 def shove(line) push(line) end |