Skip to content

Instantly share code, notes, and snippets.

@teeceepee
Created February 14, 2014 01:30
Show Gist options
  • Save teeceepee/8994219 to your computer and use it in GitHub Desktop.
Save teeceepee/8994219 to your computer and use it in GitHub Desktop.
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