Class: Dexc::RingBuffer

Inherits:
Object show all
Defined in:
lib/dexc.rb

Instance Method Summary collapse

Constructor Details

#initialize(n) ⇒ RingBuffer

Returns a new instance of RingBuffer.



13
14
15
16
17
# File 'lib/dexc.rb', line 13

def initialize(n)
  @n = n
  @buf = []
  @idx = 0
end

Instance Method Details

#add(val) ⇒ Object



19
20
21
22
# File 'lib/dexc.rb', line 19

def add(val)
  @buf[@idx] = val
  @idx = (@idx + 1) % @n
end

#to_aObject



24
25
26
# File 'lib/dexc.rb', line 24

def to_a
  @buf[@idx..-1] + @buf[0...@idx]
end