Sha256: 922889a78d638891a23c96654ee14a324cf635ae289b65c71721b4c4620ce5fa
Contents?: true
Size: 931 Bytes
Versions: 3
Compression:
Stored size: 931 Bytes
Contents
# encoding: utf-8 # frozen_string_literal: true class ReadBackwardFile attr_reader :file, :chunk_size # Create a ReadBackwardFile # # @param [File] file # the file to read backward from # @param [Integer] chunk_size # the chunk size used to step through the file backwards # # @api public def initialize(file, chunk_size = 512) @file = file @chunk_size = chunk_size @file_size = ::File.stat(file).size end # Read file in chunks # # @yield [String] # the chunk from file content # # @api public def each_chunk file.seek(0, IO::SEEK_END) while file.tell > 0 if file.tell < @chunk_size # don't read beyond file size @chunk_size = file.tell end file.seek(-@chunk_size, IO::SEEK_CUR) chunk = file.read(@chunk_size) yield(chunk) file.seek(-@chunk_size, IO::SEEK_CUR) end end end # ReadBackwardFile
Version data entries
3 entries across 3 versions & 1 rubygems
Version | Path |
---|---|
tty-file-0.6.0 | lib/tty/file/read_backward_file.rb |
tty-file-0.5.0 | lib/tty/file/read_backward_file.rb |
tty-file-0.4.0 | lib/tty/file/read_backward_file.rb |