Class: Utils::Grepper::Queue
Instance Attribute Summary collapse
-
#max_size ⇒ Integer
readonly
The max_size reader method provides access to the maximum size value.
Instance Method Summary collapse
-
#data ⇒ Array
The data method returns a duplicate of the internal data array.
-
#initialize(max_size) ⇒ Queue
constructor
The initialize method sets up a new instance with the specified maximum size and empty data array.
-
#push(x) ⇒ Queue
(also: #<<)
The push method adds an element to the queue and removes the oldest element if the maximum size is exceeded.
Constructor Details
#initialize(max_size) ⇒ Queue
The initialize method sets up a new instance with the specified maximum size and empty data array.
13 14 15 |
# File 'lib/utils/grepper.rb', line 13 def initialize(max_size) @max_size, @data = max_size, [] end |
Instance Attribute Details
#max_size ⇒ Integer (readonly)
The max_size reader method provides access to the maximum size value.
20 21 22 |
# File 'lib/utils/grepper.rb', line 20 def max_size @max_size end |
Instance Method Details
#data ⇒ Array
The data method returns a duplicate of the internal data array.
This method provides access to the internal @data instance variable by returning a shallow copy of the array, ensuring that external modifications do not affect the original data structure.
29 30 31 |
# File 'lib/utils/grepper.rb', line 29 def data @data.dup end |
#push(x) ⇒ Queue Also known as: <<
The push method adds an element to the queue and removes the oldest element if the maximum size is exceeded.
39 40 41 42 43 |
# File 'lib/utils/grepper.rb', line 39 def push(x) @data.shift if @data.size > @max_size @data << x self end |