CSV in Ruby 1.8.7 and 1.9.2

R

Ronald

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?
 
J

James Edward Gray II

Hi all,
=20
I am trying to generate a CSV with the code bellow:
=20
csv_string =3D CSV.generate:)col_sep =3D> "\t") do |csv|
csv << ["row", "of", "CSV", "data"]
csv << ["test", "a=E7=E3o"]
end
=20
=20
But when I try with Ruby 1.8.7 I get the follow error:
=20
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 =3D FCSV
else
require "csv"
end
If I try with 1.9.2 I get the error:
=20
gerar.rb:35: invalid multibyte char (US-ASCII)
gerar.rb:35: invalid multibyte char (US-ASCII)
gerar.rb:35: syntax error, unexpected $end, expecting ']'
=20
=20
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=
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top