Module: DZero::Utils::FixedWidth

Defined in:
lib/dzero/utils/fixed_width.rb

Class Method Summary collapse

Class Method Details

.parse_fixed_width(fields, string) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/dzero/utils/fixed_width.rb', line 3

def self.parse_fixed_width(fields, string)
  field_sizes   = fields.values
  field_pattern = "A#{field_sizes.join('A')}"
  headers       = fields.keys
  values        = string.unpack(field_pattern)
  Hash[headers.zip(values)]
end

.to_fixed_width(source, schema) ⇒ Object



11
12
13
14
15
16
17
# File 'lib/dzero/utils/fixed_width.rb', line 11

def self.to_fixed_width(source, schema)
  string = ''
  schema.each do |field, size|
    string = string + ("%-#{size}.#{size}s" % source[field])
  end
  return string
end