Class: RingBuffer

Inherits:
Array
  • Object
show all
Defined in:
lib/ring_buffer.rb,
lib/ring_buffer/version.rb

Constant Summary collapse

VERSION =
'1.0.0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size) ⇒ RingBuffer

Returns a new instance of RingBuffer.



8
9
10
11
12
13
# File 'lib/ring_buffer.rb', line 8

def initialize(max_size)
  raise 'max_size must be an integer' if !max_size.is_a? Integer
  raise 'max_size must be positive' if max_size < 0

  @max_size = max_size
end

Instance Attribute Details

#max_sizeObject (readonly)

Returns the value of attribute max_size.



4
5
6
# File 'lib/ring_buffer.rb', line 4

def max_size
  @max_size
end

Instance Method Details

#<<(element) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/ring_buffer.rb', line 15

def <<(element)
  return super(element) if size < @max_size

  # If the RingBuffer is full, remove the first element and add the new at the end.
  shift
  push element
end

#pushObject



6
# File 'lib/ring_buffer.rb', line 6

alias :push :<<