Created
February 14, 2014 01:30
-
-
Save teeceepee/8994219 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 Buffer | |
def initialize(initial_bytes = '') | |
@bytes = initial_bytes | |
end | |
%w[size empty? clear].each do |method| | |
define_method(method) do | |
@bytes.send(method) | |
end | |
end | |
def size | |
@bytes.size | |
end | |
def empty? | |
@bytes.empty? | |
end | |
def append(new_bytes) | |
@bytes << new_bytes | |
end | |
def retrieve(n = -1) | |
if n == -1 # retrieve all bytes | |
all = @bytes.dup | |
self.clear | |
all | |
else # retrieve n bytes | |
@bytes.slice!(0, n) | |
end | |
end | |
def peek(n = 1) | |
@bytes.slice(0, n) | |
end | |
def clear | |
@bytes.clear | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment