Created
January 22, 2016 02:43
-
-
Save gavinmcgimpsey/ecf9d636dc073136039d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CircularBuffer | |
def initialize(length) | |
@buffer = [] | |
@length = length | |
end | |
def read | |
fail BufferEmptyException.new if @buffer.empty? | |
@buffer.shift | |
end | |
def write(obj) | |
return if obj.nil? | |
fail BufferFullException.new if full? | |
@buffer.push(obj) | |
end | |
def write!(obj) | |
return if obj.nil? | |
read if full? | |
write(obj) | |
end | |
def clear | |
@buffer = [] | |
end | |
def full? | |
@buffer.length == @length | |
end | |
class BufferEmptyException < RuntimeError; end | |
class BufferFullException < RuntimeError; end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment