Nomethod 'each' error for nil class

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("\	") )

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 counties/}.each do |fname|
   meta_info = File.basename(fname, '.html').split('--')
   page = Nokogiri::HTML(open(fname))
 
 # <MY ERROR IS IN THIS BLOCK> 
 page.css("#{TABLE_DIV_ID} tr")[1..-2].each do |tr|
      data_tds = tr.css('td').map{ |td|  
         td.text.gsub(/[$,](?=\\d)/, '').gsub(/\\302\\240|\\s/, ' ').strip
      }
 # </MY ERROR IS IN THIS BLOCK>
    
data_row = meta_info + data_tds
      OFILE.puts( data_row.join("\	"))  
      DB.execute(DB_INSERT_STATEMENT, data_row)
      
 end   
end

OFILE.close

What’s the question?
You shouldn’t just post a block of code and expect us to fix it for you without telling us what the problem is :wink:

If it’s this line that’s throwing the error it means that the reject() is returning nil.


Dir.glob("data-hold/pages/*.html").reject{|f| f =~ /All counties/}.each do |fname|

try logging Dir.glob(“data-hold/pages/.html") and Dir.glob("data-hold/pages/.html”).reject{|f| f =~ /All counties/} to see what they are returning.