From: James Edward Gray II Date: 2005-10-30T08:40:12+09:00 Subject: Re: csv.rb a start on refactoring. On Oct 29, 2005, at 12:11 PM, Ara.T.Howard wrote: > ========================================== > CSV2[8] => FAILED (RuntimeError) > ========================================== > input: > "a,,," > csv: > ["a", "", "", ""] > expected: > ["a", nil, nil, nil] > ========================================== Which one of those is more correct must be debatable. I didn't see a concept of nil in the RCF document and nil stringifies to "", of course. Regardless, it's a simple fix: module CSV2 def self::parse_line( line ) csv = Array.new line.scan(/\G(?:^|,)(?:"((?>[^"]*)(?>""[^"]*)*)"|([^",]*))/) do csv << if $1.nil? if $2 == "" nil else $2 end else $1.gsub('""', '"') end end csv end end That gives us two less errors. > ========================================== > CSV2[9] => FAILED (RuntimeError) > ========================================== > input: > "," > csv: > [""] > expected: > [nil, nil] > ========================================== You're input there was strip()ed. It was actually " , ". The expression obviously fails this case either way, but in the RCF I read: "4... Spaces are considered part of a field and should not be ignored." I would interpret that to mean that the correct solution is [" ", " "]. Can you please explain why that's wrong? James Edward Gray II