Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

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

marcalc opened this Issue Oct 30, 2012 · 1 comment


None yet
2 participants

marcalc commented Oct 30, 2012

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] } }

Thanks for this great gem.


jeremyevans commented Oct 30, 2012

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