Class: Fulmar::RingBuffer

Inherits:
Array
  • Object
show all
Defined in:
lib/fulmar/ringbuffer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size, enum = nil) ⇒ RingBuffer

Returns a new instance of RingBuffer.



5
6
7
8
# File 'lib/fulmar/ringbuffer.rb', line 5

def initialize(max_size, enum = nil)
  @max_size = max_size
  enum.each { |e| self << e } if enum
end

Instance Attribute Details

#max_sizeObject (readonly)

Returns the value of attribute max_size.



3
4
5
# File 'lib/fulmar/ringbuffer.rb', line 3

def max_size
  @max_size
end

Instance Method Details

#<<(el) ⇒ Object Also known as: push



10
11
12
13
14
15
16
17
# File 'lib/fulmar/ringbuffer.rb', line 10

def <<(el)
  if self.size < @max_size || @max_size.nil?
    super
  else
    self.shift
    self.push(el)
  end
end