Class: Lich::Common::LimitedArray

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

Examples:

Creating a LimitedArray

limited_array = Lich::Common::LimitedArray.new(5)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Initializes a new LimitedArray with a specified size and an optional object.

Parameters:

  • size (Integer) (defaults to: 0)

    The initial size of the array (default is 0).

  • obj (Object) (defaults to: nil)

    An optional object to initialize the array with.



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_sizeObject

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

#historyArray

Returns an empty array representing the history.

Examples:

Getting the history of the LimitedArray

history = limited_array.history

Returns:

  • (Array)

    An empty array.



47
48
49
# File 'documented/common/limitedarray.rb', line 47

def history
  Array.new
end

#push(line) ⇒ Object

Note:

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.

Examples:

Adding an element to the LimitedArray

limited_array.push("new element")

Parameters:

  • line (Object)

    The element to add to the array.

Returns:

  • (Object)

    The element that was added to the array.



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.

Examples:

Shoving an element into the LimitedArray

limited_array.shove("another element")

Parameters:

  • line (Object)

    The element to add to the array.

Returns:

  • (Object)

    The element that was added to the array.



39
40
41
# File 'documented/common/limitedarray.rb', line 39

def shove(line)
  push(line)
end