class NMatrix::IO::FortranFormat::Reader

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


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

Public Class Methods

new(string) click to toggle source

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


  • string - FORTRAN format string to be parsed.

# File lib/nmatrix/io/fortran_format.rb, line 54
def initialize string
  @string = string

Public Instance Methods

parse() click to toggle source

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.

# 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?
    raise(IOError, "Invalid FORTRAN format specified. Only Integer, Float or Exponential acceptable.")