Trouble Parsing Custom CSV with Embedded Newlines in Ruby - Getting Incorrect Row Counts
I'm trying to debug I'm refactoring my project and I'm stuck on something that should probably be simple... This might be a silly question, but I'm working on a custom CSV parser in Ruby that needs to handle embedded newlines within quoted fields... The CSV file I'm dealing with looks like this: ``` "Name, Age, Address" "John Doe",30,"123 Main St\nSpringfield, IL" "Jane Smith",25,"456 Elm St\nSomewhere, TX" " "Bob Johnson",40,"789 Oak St\nMetropolis, NY" ``` I've been using the built-in `CSV` library, but it seems to misinterpret the newlines within the quoted fields, leading to incorrect row counts and splitting lines unexpectedly. Hereโs the code Iโve implemented so far: ```ruby require 'csv' csv_data = <<~CSV "Name, Age, Address" "John Doe",30,"123 Main St\nSpringfield, IL" "Jane Smith",25,"456 Elm St\nSomewhere, TX" "Bob Johnson",40,"789 Oak St\nMetropolis, NY" CSV CSV.foreach(StringIO.new(csv_data), headers: true) do |row| puts row.inspect end ``` When I run this code, I get the following output: ``` #<CSV::Row "Name" => "John Doe", " Age" => "30", " Address" => "123 Main St\nSpringfield, IL"> #<CSV::Row "Name" => "Jane Smith", " Age" => "25", " Address" => "456 Elm St\nSomewhere, TX"> #<CSV::Row "Name" => "Bob Johnson", " Age" => "40", " Address" => "789 Oak St\nMetropolis, NY"> ``` However, I was expecting the rows to be properly parsed without splitting the addresses. Initially, I tried setting `:liberal_parsing => true` in the options, but that didnโt help. Is there a way to correctly parse this CSV while accounting for newlines within quoted fields? Iโm using Ruby 3.0.1, and I would appreciate any insights or tips on best practices for handling such cases. What am I doing wrong? I'm coming from a different tech stack and learning Ruby. I'm on macOS using the latest version of Ruby. This is part of a larger web app I'm building. Any ideas how to fix this?