Class: Marmalade::FileReader

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

Overview

A class to help parsing out values from a Code Jam input file

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_handle) ⇒ FileReader



6
7
8
# File 'lib/marmalade/file_reader.rb', line 6

def initialize(file_handle)
  self.file_handle = file_handle
end

Instance Attribute Details

#file_handleObject

Returns the value of attribute file_handle.



4
5
6
# File 'lib/marmalade/file_reader.rb', line 4

def file_handle
  @file_handle
end

Instance Method Details

#read(assignments, options = {}) ⇒ Object

Reads the next values from the file and returns them as a hash



11
12
13
14
15
16
17
18
19
20
# File 'lib/marmalade/file_reader.rb', line 11

def read(assignments, options = {})
  line_count = options[:count]
  read_options = options.dup
  if assignments.is_a?(Array)
    read_options[:split] = true
  end
  lines = 1.upto(line_count || 1).map { read_line(file_handle, read_options) }
  lines = line_count.nil? ? lines.first : lines
  assign_results(assignments, lines)
end