Created
July 25, 2019 08:35
-
-
Save merqlove/d4671f09984852f6cd027140f34fa391 to your computer and use it in GitHub Desktop.
RUBY Buffered File Reader by Char
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 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