Skip to content

Instantly share code, notes, and snippets.

@Nimster
Forked from eerohele/ringbuffer.rb
Created November 15, 2012 11:21
Show Gist options
  • Save Nimster/4078106 to your computer and use it in GitHub Desktop.
Save Nimster/4078106 to your computer and use it in GitHub Desktop.
A simple ring buffer for Ruby.
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 self.size < @max_size || @max_size.nil?
super
else
self.shift
self.push(el)
end
end
alias :push :<<
end
@Nimster
Copy link
Author

Nimster commented Nov 15, 2012

This doesn't suffer from the bug that the original, https://gist.github.com/1904422 , suffers from (see my comment there)

@eerohele
Copy link

eerohele commented Jan 4, 2013

Thanks for the comment, I also fixed the original according to your suggestion.

@werkshy
Copy link

werkshy commented Mar 28, 2014

Thanks for posting, and thanks for fixing!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment