2010-01-26 8 views
18

J'ai un code qui fonctionne bien avec ruby ​​1.87 mais ne fonctionne pas avec ruby ​​1.9. Il est dit que CSV :: Writer n'est pas déclaré mais qu'il fait toujours partie du rdoc. Est-ce que l'API csv a changé, après la fusion de fastercsv, ou non?écrire csv dans ruby ​​1.9 et CSV :: Writer

mon code:

require 'csv' 

def self.export_csv 
file_name = File.join(RAILS_ROOT, 'public','csv',"#{start_date_f}_#{end_date_f}.csv") 
return file_name if File.exist?(file_name) 
@results = find(:all) 
header_row = [] 
outfile = File.open(file_name, 'wb') 
CSV::Writer.generate(outfile) do |csv| 
     header_row = ['gateway_id','created', 'gateway_status_id', 'panel_id', 'panel_status','volts_out', 'amps_out', 'temp','aid' ,'sid', 'pisid'] 
     csv << header_row 
    end 
end 

L'erreur que je reçois: NameError: CSV uninitialized constant :: Writer

Notez que require 'csv' est là. Je l'essaie dans ma console, quand je fais le 'csv' exige, cela fonctionne, mais dès que j'appelle CSV :: Writer je reçois cette erreur. Ce code fonctionne très bien avec ruby ​​1.87, donc cela me fait penser qu'il s'agit d'un problème de ruby ​​1.9 csv car il a été fusionné avec rapideCSV.

Répondre

43

La bibliothèque csv est toujours présente, mais pas CSV :: Writer. Selon le csv.rb en 1.9.0:

# I'm sure I'll miss something, but I'll try to mention most of the major 
# differences I am aware of, to help others quickly get up to speed: 
# 
# === CSV Parsing 
# 
# * This parser is m17n aware. See CSV for full details. 
# * This library has a stricter parser and will throw MalformedCSVErrors on 
# problematic data. 
# * This library has a less liberal idea of a line ending than CSV. What you 
# set as the <tt>:row_sep</tt> is law. It can auto-detect your line endings 
# though. 
# * The old library returned empty lines as <tt>[nil]</tt>. This library calls 
# them <tt>[]</tt>. 
# * This library has a much faster parser. 
# 
# === Interface 
# 
# * CSV now uses Hash-style parameters to set options. 
# * CSV no longer has generate_row() or parse_row(). 
# * The old CSV's Reader and Writer classes have been dropped. 
# * CSV::open() is now more like Ruby's open(). 
# * CSV objects now support most standard IO methods. 
# * CSV now has a new() method used to wrap objects like String and IO for 
# reading and writing. 
# * CSV::generate() is different from the old method. 
# * CSV no longer supports partial reads. It works line-by-line. 
# * CSV no longer allows the instance methods to override the separators for 
# performance reasons. They must be set in the constructor. 

Un peu plus tard, il y a un exemple de la façon d'écrire la ligne par ligne (ainsi que d'autres méthodes d'écriture):

# === To a File 
# 
# CSV.open("path/to/file.csv", "wb") do |csv| 
#  csv << ["row", "of", "CSV", "data"] 
#  csv << ["another", "row"] 
#  # ... 
# end 
+0

était l'approche que j'ai prise. Je viens de vérifier la réponse maintenant, de toute façon, c'est une bonne source d'information :-) –