Class: Basic101::InputReader

Inherits:
Object
  • Object
show all
Defined in:
lib/basic101/input_reader.rb

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ InputReader

Returns a new instance of InputReader.



7
8
9
10
11
12
13
14
# File 'lib/basic101/input_reader.rb', line 7

def initialize(input)
  line = input.read_line
  line += ',' unless line.empty?
  line += EOS
  @columns = line.parse_csv
rescue CSV::MalformedCSVError
  raise BadInputFormatError, 'Invalid format'
end

Instance Method Details

#read_numericObject



26
27
28
29
30
31
32
# File 'lib/basic101/input_reader.rb', line 26

def read_numeric
  column = next_column
  unless column =~ /^[+-]?\d+/
    raise BadInputFormatError, "Not numeric: #{column.inspect}"
  end
  column.to_f
end

#read_stringObject



16
17
18
19
20
21
22
23
24
# File 'lib/basic101/input_reader.rb', line 16

def read_string
  end_of_input_error if @columns.empty?
  value = @columns.shift
  if value == EOS
    ''
  else
    value || ''
  end
end