Zoltar Speaks wrote:
> Hey guys.
> 
> I am currently following an example in: "Programming Ruby: The Pragmatic
> Programmers Guide" (pg. 663) on reading CSV files with Ruby. I am
> struggling though to get the code to read a CSV file that has a header
> in it. I have copied the example exactly but get the following error:
> 
> undefined method `process' for main:Object (NoMethodError)
>         from /usr/local/lib/ruby/1.8/csv.rb:560:in `each'
>         from solution.rb:4
> 
> My Code:
> 
> require 'csv'
> reader = CSV.open("csvfile", "r")
> header = reader.shift
> reader.each{|row| process(header, row)}
>

The book has a bad habit of calling non-existent functions in the 
examples, so you have to be on the lookout for them.  As a consequence, 
a number of examples won't run as presented.  However the introduction 
(in pickaxe2, p.5) says that you can download source code for the book 
that will run, i.e. presumably the method process() will be defined in 
the downloaded source.

 I think the book should say something like this instead:

read.each do |row|
  #process row however you see fit
end

so that beginners won't get errors when they copy the examples verbatim.

-- 
Posted via http://www.ruby-forum.com/.