module Array::ToCSV
Public Instance Methods
to_csv(delimiter=",")
click to toggle source
Convert this enumerable into a CSV string (nb: enumerable must contain either all Hashes or all Arrays)
# File lib/epitools/core_ext/array.rb, line 331 def to_csv(delimiter=",") types = count_by(&:class) unless types.size == 1 and (types[Array] > 0 or types[Hash] > 0) raise "Error: this array must contain nothing but arrays, or nothing but hashes (actually contains: #{types.inspect})" end options = {} options[:col_sep] = delimiter options[:headers] = flat_map(&:keys).uniq if types[Hash] > 0 CSV.generate(nil, **options) do |csv| each { |obj| csv << obj } end end
to_tsv()
click to toggle source
Like to_csv
, but with tab-separated CSV fields
# File lib/epitools/core_ext/array.rb, line 350 def to_tsv to_csv("\t") end