1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00
mperham--sidekiq/lib/sidekiq/ring_buffer.rb

30 lines
472 B
Ruby
Raw Normal View History

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