Demo entry 2248911

Ruby

   

Submitted by anonymous on Jul 18, 2015 at 02:03
Language: Ruby. Code size: 391 Bytes.

module ActsAsCsv
  extend ActiveSupport::Concern

  module ClassMethods
    def to_csv(options = {})
      defaults = { col_sep: "\t", write_headers: true }
      CSV.generate(defaults.merge(options)) do |csv|
        all.each { |obj| csv << obj.csv_row }
      end.encode!('UTF-8', 'iso-8859-1', invalid: :replace)
    end
  end

  def csv_row
    attributes.values
  end
end

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).