Class: Utils::Grepper::Queue

Inherits:
Object
  • Object
show all
Defined in:
lib/utils/grepper.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size) ⇒ Queue

The initialize method sets up a new instance with the specified maximum size and empty data array.

Parameters:

  • max_size (Integer)

    the maximum size limit for the data storage



13
14
15
# File 'lib/utils/grepper.rb', line 13

def initialize(max_size)
  @max_size, @data = max_size, []
end

Instance Attribute Details

#max_sizeInteger (readonly)

The max_size reader method provides access to the maximum size value.

Returns:

  • (Integer)

    the maximum size value stored in the instance



20
21
22
# File 'lib/utils/grepper.rb', line 20

def max_size
  @max_size
end

Instance Method Details

#dataArray

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.

Returns:

  • (Array)

    a duplicate of the internal data array



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.

Parameters:

  • x (Object)

    the element to be added to the queue

Returns:

  • (Queue)

    returns self to allow for method chaining



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