Class: Dexc::RingBuffer

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

Instance Method Summary collapse

Constructor Details

#initialize(n) ⇒ RingBuffer

Returns a new instance of RingBuffer.



16
17
18
19
20
# File 'lib/dexc.rb', line 16

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

Instance Method Details

#add(val) ⇒ Object



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

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

#to_aObject



27
28
29
# File 'lib/dexc.rb', line 27

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