2010-01-26 14 views
18

मेरे पास एक कोड है जो रूबी 1.87 के साथ ठीक काम करता है लेकिन रूबी 1.9 के साथ काम नहीं करता है। यह कहता है कि सीएसवी :: लेखक अविकसित है लेकिन यह अभी भी rdoc का हिस्सा है। क्या सीएसवी एपीआई बदल गया है, fastcsv विलय के बाद, या नहीं?रूबी में सीएसवी लिखें 1.9 और सीएसवी :: लेखक

मेरी कोड:

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 

त्रुटि मैं प्राप्त होने वाले: NameError: अप्रारंभीकृत निरंतर सीएसवी :: लेखक

ध्यान दें कि 'सीएसवी' की आवश्यकता होती है वहाँ है। मैं इसे अपने कंसोल में आज़माता हूं, जब मैं 'सीएसवी' की आवश्यकता करता हूं, यह काम करता है, लेकिन जैसे ही मैं CSV :: लेखक को कॉल करता हूं, मुझे वह त्रुटि मिलती है। यह कोड रूबी 1.87 के साथ ठीक काम करता है, इसलिए यह मुझे लगता है कि यह एक रूबी 1.9 सीएसवी समस्या है क्योंकि इसे fastCSV के साथ विलय कर दिया गया था।

उत्तर

43

सीएसवी लाइब्रेरी अभी भी है, लेकिन सीएसवी :: लेखक नहीं है। 1.9.0 में csv.rb के अनुसार:

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

# 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. 

पर बाद में एक छोटी सी, वहाँ कैसे पंक्ति-दर-पंक्ति लिखने के लिए का एक उदाहरण (और साथ ही लेखन के अन्य तरीकों) है

+0

वह दृष्टिकोण था जिसे मैंने लिया था। मैंने अभी जवाब की जांच की है, वैसे भी, यह जानकारी का एक अच्छा स्रोत है :-) –

संबंधित मुद्दे