class IOP::FileReader

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

@since 0.1

Public Class Methods

new(file, mode: 'rb', **options) click to toggle source

Creates class instance.

@param file [String] name of file to read from

@param mode [String] open mode for the file; refer to {File} for details

@param options [Hash] keyword parameters passed to {IOReader} constructor

Calls superclass method IOP::IOReader::new
# File lib/iop/file.rb, line 73
def initialize(file, mode: 'rb', **options)
  super(nil, **options)
  @file = file
  @mode = mode
end

Public Instance Methods

process!() click to toggle source
Calls superclass method IOP::RandomAccessReader#process!
# File lib/iop/file.rb, line 79
def process!
  @io = File.new(@file, @mode)
  begin
    super
  ensure
    @io.close
  end
end