class IOP::FileWriter

Sink class to write received upstream data to a local file.

Contrary to {IOWriter}, this class manages underlying IO instance in order to close it when the process is finished even if exception is risen.

### Use case: generate 1024 bytes of random data and write it to file.

require 'iop/file'
require 'iop/securerandom'
( IOP::SecureRandomGenerator.new(1024) | IOP::FileWriter.new('random.dat') ).process!

@since 0.1

Public Class Methods

new(file, mode: 'wb') click to toggle source

Creates class instance.

@param file [String] name of file to write to

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

Calls superclass method IOP::IOWriter::new
# File lib/iop/file.rb, line 149
def initialize(file, mode: 'wb')
  super(nil)
  @file = file
  @mode = mode
end

Public Instance Methods

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