2010-01-26 3 views
18

저는 루비 1.87에서 잘 작동하지만, 루비 1.9에서는 작동하지 않습니다. 그것은 CSV :: Writer가 선언되지 않았지만 여전히 rdoc의 일부라고 말합니다. fastercsv 병합 후 csv api가 변경 되었습니까?루비 1.9와 CSV :: Writer에서 csv를 작성하십시오.

내 코드 :

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'를 필요로 초기화되지 않은 일정 CSV :: 작가

참고가있다. 내 콘솔에서 해보겠습니다. CSV :: Writer를 호출하자마자 오류가 발생합니다. 이 코드는 ruby ​​1.87에서 잘 작동하므로 더 빠른 CSV와 병합 되었기 때문에 루비 1.9 csv 문제라고 생각하게합니다.

답변

43

CSV 라이브러리는 아직 있지만 CSV :: Writer는 존재하지 않습니다. 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

내가 취한 접근이었다. 지금 막 대답을 확인했습니다. 어쨌든 좋은 정보원입니다. :-) –

관련 문제