Skip to content

Instantly share code, notes, and snippets.

@snatchev
Forked from eerohele/ringbuffer.rb
Last active December 15, 2015 17:10
Show Gist options
  • Save snatchev/5294859 to your computer and use it in GitHub Desktop.
Save snatchev/5294859 to your computer and use it in GitHub Desktop.
class RingBuffer < Array
attr_reader :max_size
def initialize(max_size, enum = nil)
@max_size = max_size
enum.each { |e| self << e } if enum
end
def <<(el)
if @max_size && self.size >= @max_size
self.shift
end
super
end
alias :push :<<
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment