mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
30 lines
472 B
Ruby
30 lines
472 B
Ruby
|
require "forwardable"
|
||
|
|
||
|
module Sidekiq
|
||
|
class RingBuffer
|
||
|
include Enumerable
|
||
|
extend Forwardable
|
||
|
def_delegators :@buf, :[], :each, :size
|
||
|
|
||
|
def initialize(size, default = 0)
|
||
|
@size = size
|
||
|
@buf = Array.new(size, default)
|
||
|
@index = 0
|
||
|
end
|
||
|
|
||
|
def <<(element)
|
||
|
@buf[@index % @size] = element
|
||
|
@index += 1
|
||
|
element
|
||
|
end
|
||
|
|
||
|
def buffer
|
||
|
@buf
|
||
|
end
|
||
|
|
||
|
def reset(default = 0)
|
||
|
@buf.fill(default)
|
||
|
end
|
||
|
end
|
||
|
end
|