Class: NMatrix::IO::FortranFormat::Reader

Inherits:
Object
  • Object
show all
Defined in:
lib/nmatrix/io/fortran_format.rb

Overview

Class for reading strings in FORTRAN format for specifying attributes of numerical data in a file. Supports F (float), E (exponential) and R (real).

Usage

p = NMatrix::IO::FortranFormat::Reader.new("(16I5)")
v = p.parse
puts v #=> { :format_code => "INT_ID", 
       #=>   :repeat      =>       16,
       #=>   :field_width =>        5 }

Instance Method Summary collapse

Constructor Details

#initialize(string) ⇒ Reader

Accepts a string in FORTRAN format and initializes the NMatrix::IO::FortranFormat::Reader object for further parsing of the data.

Arguments

  • string - FORTRAN format string to be parsed.



54
55
56
# File 'lib/nmatrix/io/fortran_format.rb', line 54

def initialize string
  @string = string
end

Instance Method Details

#parseObject

Parses the FORTRAN format string passed in initialize and returns a hash of the results.

Result Hash Format

Take note that some of the below parameters may be absent in the hash depending on the type of string being parsed.

  • :format_code - A string containing the format code of the read data.

    Can be "INT_ID", "FP_ID" or "EXP_ID"
    
  • :repeat - Number of times this format will repeat in a line.

  • :field_width - Width of the numerical part of the number.

  • :post_decimal_width - Width of the numerals after the decimal point.

  • :exponent_width - Width of exponent part of the number.

Raises:

  • (IOError)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/nmatrix/io/fortran_format.rb', line 72

def parse
  raise(IOError, "Left or right parentheses missing") if parentheses_missing? # change tests to handle 'raise' not return

  @result = {}
  @string = @string[1..-2]

  if valid_fortran_format?
    load_result
  else
    raise(IOError, "Invalid FORTRAN format specified. Only Integer, Float or Exponential acceptable.")
  end

  @result
end