Skip to content

Loading…

Export dataset to CSV using ruby's standard library. #577

Closed
marcalc opened this Issue · 1 comment

2 participants

@marcalc

This helps to fix a lot of potential problems like encoding, and commas inside strings.

class Sequel::Dataset
  require 'csv'
  def to_csv(include_column_titles = true)
    n = naked
    cols = n.columns
    csv_string = CSV.generate do |csv|
      csv << cols if include_column_titles
      n.each{|r| csv << cols.collect{|c| r[c] } }
    end
    csv_string
  end
end

Thanks for this great gem.

@jeremyevans
Owner

Your code looks fine. However, I don't want to require the csv library, as most users of Sequel do not use Dataset#to_csv. The limitations of Dataset#to_csv are documented in the RDoc, and haven't changed in probably 5 years.

I will consider adding an extension that ships with Sequel that uses this code to override Dataset#to_csv, if there is sufficient demand for it. You'll need to post on the sequel-talk Google Group seeing if anyone else wants such an extension added, and submit a real pull request with specs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.