SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Threaded View

  1. #1
    SitePoint Enthusiast
    Join Date
    May 2012
    Location
    Karkala, India
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    nomethod 'each' error for nil class

    Code Ruby:
    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 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("\t"))  
          DB.execute(DB_INSERT_STATEMENT, data_row)
     
     end   
    end
     
    OFILE.close
    Last edited by ScallioXTX; May 26, 2012 at 15:35. Reason: Applied Ruby highlighting


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •