uninitialized constant CSV::Reader

N

Nathan Sharkey

Hi,

I've searched google and these forums but can't find anything for this
error i get.

code in a file called test.rb
-----------------------------
require 'csv'

CSV::Reader.parse(File.open('stuff.csv', 'rb')) do |row|
p row
break if !row[0].is_null && row[0].data == 'stop'
end
------------------------------

ruby test.rb # gives an error

NameError: uninitialized constant CSV::Reader
from (irb):8
from /usr/local/bin/irb:12:in `<main>'

i get the same error when trying this in irb

any ideas??
Riga
 
B

Brian Candler

Nathan said:
require 'csv'

CSV::Reader.parse(File.open('stuff.csv', 'rb')) do |row| ...
NameError: uninitialized constant CSV::Reader ...
any ideas??

Perhaps you have another file called csv.rb which is earlier in your
$LOAD_PATH

(If your own program is called csv.rb, that would the the culprit :)

In any case, you'd probably be better off with the fastercsv gem.
 
N

Nathan Sharkey

Brian said:
Perhaps you have another file called csv.rb which is earlier in your
$LOAD_PATH

(If your own program is called csv.rb, that would the the culprit :)

In any case, you'd probably be better off with the fastercsv gem.

Hi, thanks for response, Fastercsv actually quits with the following
notice which made me try CSV::Reader in the first place :)

"Please switch to Ruby 1.9's standard CSV library. It's FasterCSV plus
support for Ruby 1.9's m17n encoding engine."

I havn't created any csv.rb file and I'll check loadpath but I would be
happy to use FasterCSV if anyone can suggest how i do this given the
above notice.

cheers
 
J

James Edward Gray II

=20
Hi, thanks for response, Fastercsv actually quits with the following=20=
notice which made me try CSV::Reader in the first place :)
=20
"Please switch to Ruby 1.9's standard CSV library. It's FasterCSV = plus
support for Ruby 1.9's m17n encoding engine."
=20
I havn't created any csv.rb file and I'll check loadpath but I would = be=20
happy to use FasterCSV if anyone can suggest how i do this given the=20=
above notice.

You don't want to be using it. That's why I put the error there. :)

The CSV library in Ruby 1.9 is literally the FasterCSV code. The reason =
it's preferable though is that it has been enhanced to handle Ruby =
1.9-isms, like m17n. That's why you need to switch over.

CSV::Reader is something from Ruby 1.8's version of CSV (before it was =
replaced by the FasterCSV code). You can't use that anymore with 1.9. =
Instead, you should mostly just be able to replace FasterCSV (or FSCV) =
with CSV in your code and it will work fine.

Does that make sense?

James Edward Gray II=
 
R

Robert Klemme

2010/3/31 James Edward Gray II said:
You don't want to be using it. =A0That's why I put the error there. =A0:)

The CSV library in Ruby 1.9 is literally the FasterCSV code. =A0The reaso=
n it's preferable though is that it has been enhanced to handle Ruby 1.9-is=
ms, like m17n. =A0That's why you need to switch over.
CSV::Reader is something from Ruby 1.8's version of CSV (before it was re=
placed by the FasterCSV code). =A0You can't use that anymore with 1.9. =A0I=
nstead, you should mostly just be able to replace FasterCSV (or FSCV) with =
CSV in your code and it will work fine.
Does that make sense?

I'd say yes - apart from the replacement suggestion. Obviously he is
using CSV already - just with a part of the old interface that has not
survived. :) He rather needs to adjust to the interface

CSV.foreach 'stuff.csv' do |row|
...
end

Kind regards

robert
 
J

James Edward Gray II

Obviously he is using CSV already - just with a part of the old =
interface that has not survived. :) He rather needs to adjust to the =
interface
=20
CSV.foreach 'stuff.csv' do |row|
...
end

Exactly. I assumed since he mentioned using FasterCSV, he had code like =
this somewhere he could just strip the Faster (or F) off of.

James Edward Gray II=
 
N

Nathan Sharkey

James said:
Exactly. I assumed since he mentioned using FasterCSV, he had code like
this somewhere he could just strip the Faster (or F) off of.

James Edward Gray II

hehe cheers guys that gives me a start :) I'm just learning ruby and
hadn't used either. I stumbled across FasterCSV which told me to use
CSV however i didn't think to try the faster csv example with CSV
instead i googled for CSV examples and obviously stumbled across some
for older versions :)

Thanks again for such speedy responses, off to get productive with some
csv file importing :)
 

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

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top