Class: Lich::Common::LimitedArray
- Inherits:
-
Array
- Object
- Array
- Lich::Common::LimitedArray
- Defined in:
- lib/common/limitedarray.rb
Overview
A class that extends the functionality of an Array to limit its size. When the maximum size is reached, the oldest elements are removed.
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 with a specified maximum size.
-
#push(line) ⇒ Object
Adds an element to the end of the array, removing the oldest elements if the maximum size is exceeded.
-
#shove(line) ⇒ Object
An alias for the push method.
Constructor Details
#initialize(size = 0, obj = nil) ⇒ LimitedArray
Initializes a new LimitedArray with a specified maximum size.
19 20 21 22 |
# File 'lib/common/limitedarray.rb', line 19 def initialize(size = 0, obj = nil) @max_size = 200 super end |
Instance Attribute Details
#max_size ⇒ Object
Returns the value of attribute max_size.
9 10 11 |
# File 'lib/common/limitedarray.rb', line 9 def max_size @max_size end |
Instance Method Details
#history ⇒ Array
Returns an empty array representing the history of elements.
52 53 54 |
# File 'lib/common/limitedarray.rb', line 52 def history Array.new end |
#push(line) ⇒ Object
This method modifies the array in place.
Adds an element to the end of the array, removing the oldest elements if the maximum size is exceeded.
32 33 34 35 |
# File 'lib/common/limitedarray.rb', line 32 def push(line) self.shift while self.length >= @max_size super end |
#shove(line) ⇒ Object
An alias for the push method.
43 44 45 |
# File 'lib/common/limitedarray.rb', line 43 def shove(line) push(line) end |