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

Gerald Bauer <[email protected]>
Newsgroups gmane.comp.lang.ruby.general
Message-ID <CAAxEZd-zM3+Kz7TQ6GW4i3mswjFTSbEZhsRR2o_qnczCwiLFFg@mail.gmail.com>
Hello,
   sorry for the false start of the Ruby Quiz.  The Ruby Quiz is now
back for real.

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

   Let's use the test.csv [1] file from Apache Commons CSV Reader.
The challenge: Code a parse method that passes the RubyQuizTest :-).
Start from scratch or, yes, use any library / gem you can find.

```
def parse( txt )
  ...
end
```

To qualify for solving the code challenge / puzzle you must pass the test:

```
require 'minitest/autorun'


class RubyQuizTest < MiniTest::Test

  def test_parse

    records = [["A", "B", "C", "D"],
              ["a", "b", "c", "d"],
              ["e", "f", "g", "h"],
              [" i ", " j ", " k ", " l "],
              ["", "", "", ""],
              ["", "", "", ""]]


    assert_equal records, parse( <<TXT )
A,B,C,"D"
# plain values
a,b,c,d
# spaces before and after
 e ,f , g,h
# quoted: with spaces before and after
" i ", " j " , " k "," l "
# empty values
,,,
# empty quoted values
"","","",""
# 3 empty lines



# EOF on next line
TXT

  end # method test_parse
end # class RubyQuizTest
```

Post your code snippets here on the "official" Ruby Quiz Channel,
that is, the ruby-talk mailing list :-).

Happy hacking and data wrangling with Ruby.

[1]: https://github.com/apache/commons-csv/blob/master/src/test/resources/CSVFileParser/test.csv

Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.