Re: Ruby Quiz #1 - Bonus - Three More Difficulty Levels (2, 3, 4) - Commas Inside Quotes and Double Up Quotes in Quotes, ...

"Frank J. Cameron" <[email protected]>
Newsgroups gmane.comp.lang.ruby.general
Message-ID <[email protected]>
Gerald Bauer wrote:
>     Thanks for the first three entries - all passing the level 1 test.
> Yay!
>     If that was too easy :-)  I added three more difficulty levels
>   to the challenge.[1]

$ ruby lib/001.rb
Run options: --seed 639
# Running:
.F..
Finished in 0.007014s, 570.3056 runs/s, 570.3056 assertions/s.
  1) Failure:
RubyQuizTest#test_parse_level4 [/home/fjc/ruby-quiz/001/test.rb:68]:
--- expected
+++ actual
@@ -1 +1 @@
-[[1, "Hamlet says, 'Seems,' madam! Nay it is; I know not 'seems.'"], [2,
"Hamlet says, \"Seems,\" madam! Nay it is; I know not \"seems.\""]]
+[["1", "Hamlet says, 'Seems,' madam! Nay it is; I know not 'seems.'"],
["2", "Hamlet says, \"Seems,\" madam! Nay it is; I know not \"seems.\""]]
4 runs, 4 assertions, 1 failures, 0 errors, 0 skips

$ cat lib/001.rb
require_relative '../001/test.rb'
class RubyQuizTest
  def parse(text)
    text.lines.map do |line|
      next if line.match(/^\s*$|#/)
      line
      .strip
      .gsub(/"(([^"\\]|"")*(\\.([^"\\]|"")*)*)"|\'([^\'\\]*(\\.[^\'\\]*)*)\'/){
        "\000#{
          Regexp.last_match
          .to_a[1..-1]
          .compact[0]
          .gsub(/,/, "\001")
          .gsub(/\\"|""/, '"')
        }\000"
      }
      .split(/\s*,\s*/, -1)
    end.compact.map do |cells|
      cells.map do |cell|
        cell
        .gsub(/\001/, ',')
        .gsub(/\000/, '')
      end
    end
  end
end
RubyQuizTest.new('fjc')



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.