Class: IOP::FileReader
Overview
Feed class to read data from local file and send it in blocks downstream.
Contrary to IOReader, this class manages underlying IO instance in order to close it when the process is finished even if exception is risen.
### Use case: compute MD5 hash sum of the first 1024 bytes of a local file.
require 'iop/file'
require 'iop/digest'
( IOP::FileReader.new('input.dat', size: 1024) | (d = IOP::DigestComputer.new(Digest::MD5.new)) ).process!
puts d.digest.hexdigest
Instance Attribute Summary
Attributes included from Feed
Instance Method Summary collapse
-
#initialize(file, mode: 'rb', **options) ⇒ FileReader
constructor
Creates class instance.
- #process! ⇒ Object
Methods included from Feed
Constructor Details
#initialize(file, mode: 'rb', **options) ⇒ FileReader
Creates class instance.
98 99 100 101 102 |
# File 'lib/iop/file.rb', line 98 def initialize(file, mode: 'rb', **) super(nil, **) @file = file @mode = mode end |
Instance Method Details
#process! ⇒ Object
104 105 106 107 108 109 110 111 |
# File 'lib/iop/file.rb', line 104 def process! @io = File.new(@file, @mode) begin super ensure @io.close end end |