Class: Lich::Common::LimitedArray

Inherits:
Array
  • Object
show all
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.

See Also:

  • Array

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size = 0, obj = nil) ⇒ LimitedArray

Initializes a new LimitedArray instance.

Parameters:

  • size (Integer) (defaults to: 0)

    the initial size of the array (default is 0)

  • obj (Object, nil) (defaults to: nil)

    the initial object to populate the array (default is nil)



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_sizeObject

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

#historyArray

Returns an empty array representing the history of elements.

Returns:

  • (Array)

    an empty array



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.

Parameters:

  • line (Object)

    the element to add to the array

Returns:

  • (Object)

    the element that was added



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.

Parameters:

  • line (Object)

    the element to add to the array

Returns:

  • (Object)

    the element that was added



34
35
36
# File 'documented/common/limitedarray.rb', line 34

def shove(line)
  push(line)
end