Class: Console::Mux::RollingArray

Inherits:
Array
  • Object
show all
Defined in:
lib/console/mux/rolling_array.rb

Overview

Array that limits itself to some fixed size. Newly added elements kick out the oldest element when at the limit.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(maxsize) ⇒ RollingArray

Returns a new instance of RollingArray.

Parameters:

  • maxsize

    the maximum size of the array



29
30
31
# File 'lib/console/mux/rolling_array.rb', line 29

def initialize(maxsize)
  @maxsize = maxsize
end

Instance Attribute Details

#maxsizeObject

Returns the value of attribute maxsize.



26
27
28
# File 'lib/console/mux/rolling_array.rb', line 26

def maxsize
  @maxsize
end

Instance Method Details

#<<(other) ⇒ Object

Append an element to the array. If the size of the array would overflow the maxsize, the array is shifted.



35
36
37
38
# File 'lib/console/mux/rolling_array.rb', line 35

def <<(other)
  super
  shift if size > maxsize
end