Class: IOP::FileReader
- Inherits:
-
IOReader
- Object
- RandomAccessReader
- IOReader
- IOP::FileReader
- Defined in:
- lib/iop/file.rb
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 inherited from RandomAccessReader
Methods included from Feed
Constructor Details
#initialize(file, mode: 'rb', **options) ⇒ FileReader
Creates class instance.
73 74 75 76 77 |
# File 'lib/iop/file.rb', line 73 def initialize(file, mode: 'rb', **) super(nil, **) @file = file @mode = mode end |
Instance Method Details
#process! ⇒ Object
79 80 81 82 83 84 85 86 |
# File 'lib/iop/file.rb', line 79 def process! @io = File.new(@file, @mode) begin super ensure @io.close end end |