Re: Ruby Quiz is Back - Challenge #1 - Read Comma-Separated Values (CSV) from the "Real World"


(Frank J. Cameron) #1

Challenge #1 - Read Comma-Separated Values (CSV) from the "Real World"

$ ruby lib/1.rb
Run options: --seed 19056
# Running:
.
Finished in 0.002289s, 436.9176 runs/s, 436.9176 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

$ cat lib/1.rb
require_relative '../test/1.rb'
class RubyQuizTest
  def parse(text)
    text.lines.map do |line|
      next if line.match(/^\s*$|#/)
      line.strip.split(/\s*,\s*/, -1)
    end.compact.map do |cells|
      cells.map do |cell|
        cell.match(/"(.*)"/) ? $1 : cell
      end
    end
  end
end
RubyQuizTest.new('fjc')