Method: Array#to_csv
- Defined in:
- lib/array/formatter.rb
#to_csv ⇒ Object
string = ARRAY.to_csv
Convert an array of arrays to CSV representation Basically, each top-level array row becomes a line of CSV data The 2nd level arrays become individual rows of data, separated by commas. Each 2nd level array item gets quoted if it contains any punctuation characters.
55 56 57 |
# File 'lib/array/formatter.rb', line 55 def to_csv self.map {|row| row.map {|f| f =~ /[[:punct:]]/ ? '"' + f.gsub(/"/, '""') + '"' : f }.join(",")}.join("\n") end |