Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 26th, 2012, 10:23 AM
bhatpajju13 bhatpajju13 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 4 bhatpajju13 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 8 sec
Reputation Power: 0
nomethoderror "each" 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("\t") )

DBNAME = "data-hold/tel-directory.sqlite"
File.delete(DBNAME) if File.exists?DBNAME
DB = SQLite3:atabase.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(/[$,](?=\d)/, '').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

Reply With Quote
  #2  
Old May 27th, 2012, 02:12 AM
sepp2k1 sepp2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 75 sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 23 h 21 m 30 sec
Reputation Power: 38
Next time it would help if you'd tell us which line the error happens on (and use proper code tags). But from the looks of it there's only one line here where the expression you're calling each on might return nil:

Code:
page.css("#{TABLE_DIV_ID} tr")[1..-2].each do


some_array[1..-2] will return nil if some_array is empty (yes, seriously), so 'page.css("#{TABLE_DIV_ID} tr")' apparently returns an empty array and that's why you get the error.

Reply With Quote
  #3  
Old May 27th, 2012, 08:22 AM
bhatpajju13 bhatpajju13 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2012
Posts: 4 bhatpajju13 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 38 m 8 sec
Reputation Power: 0
Quote:
Originally Posted by sepp2k1
Next time it would help if you'd tell us which line the error happens on (and use proper code tags). But from the looks of it there's only one line here where the expression you're calling each on might return nil:

Code:
page.css("#{TABLE_DIV_ID} tr")[1..-2].each do


some_array[1..-2] will return nil if some_array is empty (yes, seriously), so 'page.css("#{TABLE_DIV_ID} tr")' apparently returns an empty array and that's why you get the error.


could you tell me what to change over there please i am tired any idea .............i am newbie who just began learning ruby.

Reply With Quote
  #4  
Old May 27th, 2012, 11:52 AM
sepp2k1 sepp2k1 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2012
Posts: 75 sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level)sepp2k1 User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 23 h 21 m 30 sec
Reputation Power: 38
Well that would depend. Is it expected that 'page.css("#{TABLE_DIV_ID} tr")' might return an empty array? If it isn't, you should investigate why it did in this case. If it is, you should check whether the array is empty before calling [1..-2] on it.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Error in each method in following code

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap