CSV in Ruby 1.8.7 and 1.9.2

Hi all,

I am trying to generate a CSV with the code bellow:

csv_string = CSV.generate(:col_sep => "\t") do |csv|
    csv << ["row", "of", "CSV", "data"]
    csv << ["test", "ação"]
end

But when I try with Ruby 1.8.7 I get the follow error:

ruby-1.8.7-p249/lib/ruby/1.8/csv.rb:326:in `initialize': can't convert
Hash into String (TypeError)

And if I try to call the generate methos without arguments, I get a
error too

If I try with 1.9.2 I get the error:

gerar.rb:35: invalid multibyte char (US-ASCII)
gerar.rb:35: invalid multibyte char (US-ASCII)
gerar.rb:35: syntax error, unexpected $end, expecting ']'

How to fix this?

Hi all,

I am trying to generate a CSV with the code bellow:

csv_string = CSV.generate(:col_sep => "\t") do |csv|
   csv << ["row", "of", "CSV", "data"]
   csv << ["test", "ação"]
end

But when I try with Ruby 1.8.7 I get the follow error:

ruby-1.8.7-p249/lib/ruby/1.8/csv.rb:326:in `initialize': can't convert
Hash into String (TypeError)

That's because Ruby 1.8.7 has a different CSV library. You'll need to use FasterCSV there to get the same interface. Change your require to something like:

if RUBY_VERSION < "1.9"
  require "rubygems"
  require "faster_csv"
  CSV = FCSV
else
  require "csv"
end

If I try with 1.9.2 I get the error:

gerar.rb:35: invalid multibyte char (US-ASCII)
gerar.rb:35: invalid multibyte char (US-ASCII)
gerar.rb:35: syntax error, unexpected $end, expecting ']'

How to fix this?

This is a non-CSV related complaint about your code. On line 35 of the gerar.rb file you have used a non-ASCII character. To do this in Ruby 1.9, you must specify the encoding of your file. If it's UTF-8, making this the first line of the file should fix it:

  # encoding: UTF-8

Hope that helps.

James Edward Gray II

···

On Feb 18, 2011, at 10:25 AM, Ronald wrote: