ruby1.9中CSV.open的API变化
参考博文:
http://comments.gmane.org/gmane.comp.lang.ruby.japanese/5658
http://permalink.gmane.org/gmane.comp.lang.ruby.japanese/5662
?
有一个文本文件test.txt (或者test.csv),其内容为:
Fred Bloggs,Manager,Male,45Laura Smith,Cook,Female,23Debbie Watts,Professor,Female,38
?
Ruby 代码:
#!/usr/bin/rubyrequire 'csv'# CSV.open('test.csv','r') do |person|CSV.open('test.txt','r') do |person| # p person puts person.inspectend
?
按照书中的示例,代码应该打印三个数组,??
["Fred Bloggs", "Manager", "Male", "45"]["Laura Smith", "Cook", "Female", "23"]["Debbie Watts", "Professor", "Female", "38"]
?
但实际的运行结果如下:
输出:
<#CSV io_type:File io_path:"test.txt" encoding:GBK lineno:0 col_sep:"," row_sep:"\r\n" quote_char:""">
?
后来经过调查得知,自己前不久将ruby的版本从1.8升级至了→
ruby 1.9.2p180 (2011-02-18 revision 30909) [i686-linux],
?
而ruby1.9中CSV.open的API发生了变更,在ruby1.9中要想打印或者输出csv/txt的文件内容,
需要按照如下的写法:
require 'csv'CSV.open(...) do |csv| csv.each do |row| p row endend
?
?
?
?
?