Class: RubyUtils::FileReader

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_utils/file_reader.rb

Overview

Simple file reader using the csv library to read a csv file requires csv

Raises:

  • (IOError)

    if csv throws an exception

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, delimiter) ⇒ FileReader

initialization

Parameters:

  • filename (String)

    filepath of the file which should be read

  • delimiter (String)

    the column delimiter that is need to read the file

Raises:

  • (IOError)

    if an error occurs while the file is read



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/ruby_utils/file_reader.rb', line 18

def initialize(filename, delimiter)
  begin
    @data = CSV.read(filename, col_sep: delimiter)
    # remove nil entries
    @data.each { |line|
      line.compact!
    }
  rescue StandardError => e
    raise IOError, e.message.concat(".").red
  end
end

Instance Attribute Details

#dataArray (readonly)

Returns an array containing the read data.

Returns:

  • (Array)

    an array containing the read data



12
13
14
# File 'lib/ruby_utils/file_reader.rb', line 12

def data
  @data
end