Error in "each" method in following code

require ‘rubygems’
require ‘nokogiri’
require ‘sqlite3’

FIELD_NAMES = [[‘selectcity’, ‘VARCHAR’],[‘match’, ‘VARCHAR’],
[‘phone_no’, ‘NUMERIC’], [‘name’, ‘VARCHAR’],[‘address’, ‘VARCHAR’] ]

TABLE_DIV_ID = “#dgrSearch
OFILE = File.open(‘data-hold/tel-directory.txt’, ‘w’)
OFILE.puts( FIELD_NAMES.map{|f| f[0]}.join("\t") )

DBNAME = “data-hold/tel-directory.sqlite”
File.delete(DBNAME) if File.exists?DBNAME
DB = SQLite3::Database.new( DBNAME )

TABLE_NAME = “telephone_records”
DB_INSERT_STATEMENT = “INSERT into #{TABLE_NAME} values
(#{FIELD_NAMES.map{’?’}.join(’,’)})”

DB.execute “CREATE TABLE #{TABLE_NAME}(#{FIELD_NAMES.map{|f| "#{f[0]}
#{f[1]}”}.join(’, ')});"
FIELD_NAMES.each do |fn|
DB.execute “CREATE INDEX #{fn[2]} ON #{TABLE_NAME}(#{fn[0]})” unless
fn[2].nil?
end

Dir.glob(“data-hold/pages/*.html”).reject{|f| f =~ /All match/}.each do
|fname|
meta_info = File.basename(fname, ‘.html’).split(’–’)
page = Nokogiri::HTML(open(fname))

page.css("#{TABLE_DIV_ID} tr")[1…-2].each do |tr|
data_tds = tr.css(‘td’).map{ |td|
td.text.gsub(/$,/, ‘’).gsub(/\302\240|\s/, ’ ').strip
}

data_row = meta_info + data_tds
OFILE.puts( data_row.join("\t"))
DB.execute(DB_INSERT_STATEMENT, data_row)

end
end

OFILE.close

On 26 May 2012 16:46, Prajwal B. [email protected] wrote:

FIELD_NAMES.each do |fn|
data_tds = tr.css(‘td’).map{ |td|
OFILE.close
It might be easier to help if you told us what the error is, and which
line is causing the problem.

Colin

Cut it down to the minimun necessary to show the problem. There are
several
each calls here.

Jeffrey

Quoting Prajwal B. [email protected]:

no method error “each” for nil class

page.css("#{TABLE_DIV_ID} tr")[1…-2].each do |tr|
data_tds = tr.css(‘td’).map{ |td|
td.text.gsub(/$,/, ‘’).gsub(/\302\240|\s/, ’ ').strip
}

On 26 May 2012 18:50, Prajwal B. [email protected] wrote:

page.css(“#{TABLE_DIV_ID} tr”)[1…-2].each do |tr|
data_tds = tr.css(‘td’).map{ |td|
td.text.gsub(/$,/, ‘’).gsub(/\302\240|\s/, ’ ').strip
}

first line is saying no method error ‘each’ do
for nil class

Then page.css(“#{TABLE_DIV_ID} tr”)[1…-2] is nil

Colin

page.css("#{TABLE_DIV_ID} tr")[1…-2].each do |tr|
data_tds = tr.css(‘td’).map{ |td|
td.text.gsub(/$,/, ‘’).gsub(/\302\240|\s/, ’ ').strip
}

first line is saying no method error ‘each’ do
for nil class

On 27 May 2012 06:31, Prajwal B. [email protected] wrote:

Dir.glob(“data-hold/pages/*.html”).each do |fname|
meta_info = File.basename(fname, ‘.html’).split(‘–’)
page = Nokogiri::HTML(open(fname))

page.css(“#{TABLE_DIV_ID} tr”)[1…-2].each do |tr|
data_tds = tr.css(td).map{ |td|
td.text.gsub(/$,/, ‘’).gsub(/\302\240|\s/, ‘’).strip
}

error in each do method in first line of both blocks

Why have you posted this again rather than continuing with previous
thread? As I said previously the object you are calling .each on must
be nil. Have a look at the Rails Guide on Debugging for suggestions
as to how to debug the code to find out why.

Colin

Dir.glob(“data-hold/pages/*.html”).each do |fname|
meta_info = File.basename(fname, ‘.html’).split(’–’)
page = Nokogiri::HTML(open(fname))

page.css("#{TABLE_DIV_ID} tr")[1…-2].each do |tr|
data_tds = tr.css(td).map{ |td|
td.text.gsub(/$,/, ‘’).gsub(/\302\240|\s/, ‘’).strip
}

error in each do method in first line of both blocks

block irb binding in second block first line