Skip to content

Instantly share code, notes, and snippets.

@merqlove
Created July 25, 2019 08:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merqlove/d4671f09984852f6cd027140f34fa391 to your computer and use it in GitHub Desktop.
Save merqlove/d4671f09984852f6cd027140f34fa391 to your computer and use it in GitHub Desktop.
RUBY Buffered File Reader by Char
class FileIO
def initialize(filename, buffer_size: 50)
@filename = filename
@buffer = ""
@buffer_size = buffer_size
end
def each(&block)
File.open(@filename, 'r') do |f|
f.each_char do |c|
@buffer += c
if @buffer.size >= @buffer_size
block.call(@buffer)
@buffer = ""
end
end
block.call(@buffer)
@buffer = ""
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment