Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Enumerable to reader draft
module Enumerable
class Reader
def initialize(enumerable)
@enumerable = enumerable
@buffer = "".b
end
def read(nsize, buffer = nil)
@iterator ||= @enumerable.enum_for(:each)
(buffer ||= @buffer).clear
if @rest
buffer << @rest
@rest.clear
end
while buffer.bytesize < nsize
begin
buffer << @iterator.next.force_encoding(Encoding::BINARY)
rescue StopIteration
return if buffer.empty?
break
end
end
@rest = buffer.slice!(nsize..-1)
buffer
end
end
def to_reader
Reader.new(self)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.