From: Zoltar Speaks Date: 2007-11-02T23:28:20+09:00 Subject: Re: CSV with Ruby Newbie Adam Boyle wrote: > Zoltar Speaks wrote: >> undefined method `process' for main:Object (NoMethodError) >> from /usr/local/lib/ruby/1.8/csv.rb:560:in `each' >> from solution.rb:4 > > Does the book define a "process" function? The code below would require > you to define a function process that does whatever it is you need to do > with the header. > >> reader.each{|row| process(header, row)} > > ex- > def process( header, row ) > # do something with header > # do something with row > end > > If process is a method of the CSV module, you'd need to prefix it with > CSV:: to get it to work. Hmmmm no there is no definition of the function 'process' in the book, i can't see header as being a CSV module either. The book starts simply with: CSV... Count, Description, Price 12,eggs,2.89, 2,"shirt, blue",21.45,special 1,"""Hello Kitty"" bag",13.99 Ruby... require 'csv' CSV.open("csvfile", "r") do |row| qty = row[0].to_i price = row[2].to_f printf "%20s: $%5.2f %s\n", row[1], qty*price, row[3] || " ---" end Output... Description: $ 0.00 --- eggs: $34.68 --- shirt, blue: $42.90 special "Hello Kitty" bag: $13.99 --- It then says: "Some CSV files have a header line. Read it, and then process the rest of the file." and the Ruby provided is: require 'csv' reader = CSV.open("csvfile", "r") header = reader.shift reader.each {|row| process(header, row)} -- Posted via http://www.ruby-forum.com/.