steel steel - 7 months ago 47
Ruby Question

Split string into an array by comma, unless comma is inside quotes

Given a string of an array in Ruby with some items in quotes that contain commas:

# => "\"hey, you\", 21"

How can I get an array of:

["hey, you", " 21"]


The Ruby standard CSV library's .parse_csv, does exactly this.

require 'csv'
"\"hey, you\", 21".parse_csv
# => ["hey, you", " 21"]