Ruby CSV转换错误

时间:2014-11-30 00:57:14

标签: ruby-on-rails ruby csv scripting type-conversion

我只是想获得冒号分隔文件的平均两列。我在这里做错了什么?

/experimental/1$ cat list.csv
a:7:98:East
b:2:34:East
c:10:94:North
d:7:43:West
e:6:88:South

/experimental/1$ cat play.rb 
#!/usr/bin/env ruby

require 'csv'
average_spending = Array.new
CSV.foreach('list.csv', converters: :numeric, { :col_sep => ':' }) do |row|
  average_spending << row[2] / row[1]
end

/experimental/1$ ./play.rb
./play.rb:5: syntax error, unexpected ')', expecting tASSOC
... :numeric, { :col_sep => ':' }) do |row|
...                               ^
./play.rb:7: syntax error, unexpected keyword_end, expecting $end

提前谢谢你,

〜克里斯

1 个答案:

答案 0 :(得分:0)

CSV.foreach接受2个参数并阻止(你试图传递3),使用

CSV.foreach('list.csv', { converters: :numeric, col_sep: ':' }) {...}