1

How to append to a array of hashes into CSV in Ruby 1.8. There is FasterCSV for Ruby 1.9 but how do I do in 1.8?

This is what I have tried. hasharray is an array which contains elements which are hashes.

CSV.open("data.csv", "wb") { |csv|
  hasharray.each{ |oput|
    oput.to_a.each {|elem| csv << elem}
  }
}

This way puts all the data in the CSV but it puts them one below another instead of side-by-side.

4

1 回答 1

1

迭代哈希时,您希望在块中使用两个参数,一个用于键,另一个用于值。考虑:

hasharray.each { |k,v| puts "#{k},#{v}" }
于 2012-06-19T23:11:53.773 回答