Class: Lich::Common::LimitedArray
- Inherits:
-
Array
- Object
- Array
- Lich::Common::LimitedArray
- Defined in:
- documented/common/limitedarray.rb
Overview
Represents an array with a limited maximum size. This class extends the standard Array class to enforce a maximum size. When the limit 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.
-
#initialize(size = 0, obj = nil) ⇒ LimitedArray
constructor
Initializes a new LimitedArray with a specified size and an optional object.
-
#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
Adds an element to the end of the array, same as push.
Constructor Details
#initialize(size = 0, obj = nil) ⇒ LimitedArray
Initializes a new LimitedArray with a specified size and an optional object.
18 19 20 21 |
# File 'documented/common/limitedarray.rb', line 18 def initialize(size = 0, obj = nil) @max_size = 200 super end |
Instance Attribute Details
#max_size ⇒ Object
Returns the value of attribute max_size.
12 13 14 |
# File 'documented/common/limitedarray.rb', line 12 def max_size @max_size end |
Instance Method Details
#history ⇒ Array
Returns an empty array representing the history.
47 48 49 |
# File 'documented/common/limitedarray.rb', line 47 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.
29 30 31 32 |
# File 'documented/common/limitedarray.rb', line 29 def push(line) self.shift while self.length >= @max_size super end |
#shove(line) ⇒ Object
Adds an element to the end of the array, same as push.
39 40 41 |
# File 'documented/common/limitedarray.rb', line 39 def shove(line) push(line) end |