This blog is part of our Ruby 2.4 series.
Comma-Separated Values (CSV) is a widely used data format and almost every language has a module to parse it. In Ruby, we have CSV class to do that.
According to RFC 4180, we cannot have unescaped double quotes in CSV input since such data can't be parsed.
We get MalformedCSVError error when the CSV data does not conform to RFC 4180.
Ruby 2.4 has added a
liberal parsing option to parse such
bad data. When it is set to true, Ruby will try to parse the data even when
the data does not conform to RFC 4180.
# Before Ruby 2.4
> CSV.parse_line('one,two",three,four')
CSV::MalformedCSVError: Illegal quoting in line 1.
# With Ruby 2.4
> CSV.parse_line('one,two",three,four', liberal_parsing: true)
=> ["one", "two\"", "three", "four"]

Written byErshad Kunnakkadan
Related blogs
- 01Ruby 2.4 has optimized enumerable min max methodsSeptember 28, 2017 · Chirag Shah
- 02CSV::Row#each etc. return enumerator when no block givenSeptember 25, 2017 · Sushant Mittal
- 03Ruby 2.4 DateTime#to_time & Time#to_time keeps infoSeptember 19, 2017 · Sushant Mittal
- 04Avoid exception for dup on IntegerAugust 1, 2017 · Rohit Arolkar