Class: FileReverseReader
- Inherits:
-
Object
- Object
- FileReverseReader
- Defined in:
- lib/file_reverse_reader.rb
Instance Attribute Summary collapse
-
#io ⇒ Object
readonly
Returns the value of attribute io.
-
#step ⇒ Object
readonly
Returns the value of attribute step.
Instance Method Summary collapse
- #binary_file? ⇒ Boolean
- #each_line(&block) ⇒ Object
-
#initialize(io, step = 1024 * 1024 * 1) ⇒ FileReverseReader
constructor
A new instance of FileReverseReader.
- #tail(limit = 10) ⇒ Object
Constructor Details
#initialize(io, step = 1024 * 1024 * 1) ⇒ FileReverseReader
Returns a new instance of FileReverseReader.
4 5 6 7 |
# File 'lib/file_reverse_reader.rb', line 4 def initialize(io, step = 1024 * 1024 * 1) @io = io @step = step end |
Instance Attribute Details
#io ⇒ Object (readonly)
Returns the value of attribute io.
2 3 4 |
# File 'lib/file_reverse_reader.rb', line 2 def io @io end |
#step ⇒ Object (readonly)
Returns the value of attribute step.
2 3 4 |
# File 'lib/file_reverse_reader.rb', line 2 def step @step end |
Instance Method Details
#binary_file? ⇒ Boolean
35 36 37 38 39 40 |
# File 'lib/file_reverse_reader.rb', line 35 def binary_file? sample = io.read(1024) || "" !sample.force_encoding('utf-8').valid_encoding? ensure io.rewind end |
#each_line(&block) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/file_reverse_reader.rb', line 9 def each_line(&block) #read from the end of file io.seek(0, IO::SEEK_END) buf = "" loop do if reach_start_of_file? read_rest(buf, &block) break end read_to_buf_by_step(buf) #if buffer dose not include multi lines, seek more. if buf[$/].nil? next else split_only_whole_lines(buf, &block) buf = "" end end end |
#tail(limit = 10) ⇒ Object
31 32 33 |
# File 'lib/file_reverse_reader.rb', line 31 def tail(limit = 10) enum_for(:each_line).first(limit).reverse end |