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.



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

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

Instance Method Details

#read_numericObject



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

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

#read_stringObject



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

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