Ruby. Crack gem. -- in `<main>': undefined method `[]' for nil:NilClass (NoMethodError) -- - ruby

Dear stackoverflow community,
Beginner's question:
Why do I get the following error?
scraper_sample_2.rb:7:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)
>Exit code: 1
Here's my code (copied from a ruby's intro guide):
require "rubygems"
require "crack"
require "open-uri"
URL = "http://www.recovery.gov/pages/GetXmlData.aspx?data=recipientHomeMap"
Crack::XML.parse(open(URL).read)["totals"]["state"].each do |state|
puts ["id", "awarded", "received", "jobs"].map{|f| state[f]}.join(",")
end

Because Crack::XML.parse(open(URL).read)["totals"] is nil. Try to split the call you do on line 7 on several lines and debug each call separately. Maybe the answer you get is not what you expect.

Given the format of the xml returned from your source, Crack::XML.parse(open(URL).read)["totals"] will, as Ivaylo said, return nil. The format of the xml must have changed, as totals are now within /map/view.
To get the expected output, change your code to:
Crack::XML.parse(open(URL).read)["map"]["view"]["totals"]["state"].each do |state|
puts ["id", "awarded", "received", "jobs"].map{|f| state[f]}.join(",")
end

Related

Figure out where a method was defined

If I follow the following part of this article:
Figure out where a method was defined
object = Object.new
puts object.method(:blank?).source_location
=> ["/gems/activesupport-5.0.0.beta1/lib/active_support/core_ext/object/blank.rb", 14]
I should be able to find the definition of the blank? method, however when I try this code within irb with ruby 2.0.0 I get this error message:
➜ ~ irb
irb(main):001:0> object = Object.new
=> #<Object:0x007fc84882f088>
irb(main):002:0> puts object.method(:blank?).source_location
NameError: undefined method `blank?' for class `Object'
from (irb):2:in `method'
from (irb):2
from /usr/bin/irb:12:in `<main>'
Did I miss anything?
Thank you.
.blank? method does not exist for a Object type. I know for sure it exists for a String method if I include the active_support lib
irb(main):001:0> String.new.method(:blank?).source_location
=> ["/home/xeon/.rbenv/versions/2.3.4/lib/ruby/gems/2.3.0/gems/activesupport-4.2.8/lib/active_support/core_ext/object/blank.rb", 116]
If you include activesupport-5.0.0.beta1 then it will work for you. (Looking at the source path of the article you have posted)

undefined method data_for for main:object error in Ruby

I wrote a small script that try to read data from a .yml file using DataMagic.
require 'watir-webdriver'
require 'data_magic'
DataMagic.yml_directory="/home/krishna/RUBY/"
DataMagic.load "testdata.yml"
testData = data_for("testdata.yml/Testcase_01")
puts "#{testData[:username]}"
when i execute this i am getting an error
hashdata.rb:7:in `<main>': undefined method `data_for' for main:Object (NoMethodError)
Tell me what i am missing. Thanks.
require 'watir-webdriver'
require 'data_magic'
include DataMagic
DataMagic.yml_directory="/home/krishna/RUBY/"
testData = data_for("testdata/Testcase_01")
puts "#{testData['username']}"

undefined method `[]' for nil:NilClass while using Nokogiri

I am using Nokogiri to scrape data from a HTML document, but I'm running into the following error:
`block in <main>': undefined method `[]' for nil:NilClass (NoMethodError)
This is the code to reproduce the problem:
require 'rubygems'
require 'nokogiri'
require 'open-uri'
url = "http://www.somewebsite.com/somepage/some"
doc = Nokogiri::HTML(open(url))
puts doc.at_css("title").text
doc.css(".Info_listing").each do |x|
puts x.at_css(".MoreInfo")[:href]
end
Does anyone know why I'm getting this error?
at_css will return nil if there's no matching element.
If you want to get MoreInfo class element inside Info_listing-class element, you'd better to use following code:
doc.css(".Info_listing .MoreInfo").each do |x|
puts x[:href]
end

Error in first attempt Ruby webcrawler

I am creating a basic scraper that gets the total relief amount rewarded to each state and then displays it, but I'm receiving an error I don't understand. Can you help me fix my program please?
require 'rubygems'
require 'crack'
require 'open-uri'
URL = 'http://www.recovery.gov/pages/GetXmlData.aspx?data=recipientHomeMap'
Crack::XML.parse(open(URL).read)['totals']['state'].each do |state|
puts ['id', 'awarded', 'received', 'jobs'].map{|f| state[f]}.join(', ')
end
rb:7:in ' : undefined method '[]' for nil:NilClass(NoMethodError)
Check what Crack::XML.parse(open(URL).read) return
You aren't getting anything back from Crack::XML.parse(open(URL).read)
You are trying to access values from nil hence the undefined method '[]' for nil:NilClass
Make sure you are actually getting the file first.

Undefined method `name' for nil:NilClass (NoMethodError) when running script

When I run the following script to retrieve the first page of google results
#!/usr/bin/env ruby
require 'rubygems'
require 'nokogiri'
require 'open-uri'
doc = Nokogiri::HTML(open('http://www.google.co.uk/search?q=stackoverflow'))
doc.css('div.vsc').each do |element|
puts element.at_css("h3.r a.l").content
end
I get a undefined methodcontent' for nil:NilClass (NoMethodError)`
How could I solve that? Or at least how could avoid it showing when executing?
As Dave Newton already pointed out in his comment, the result of at_css("h3.r a.l") is nil in your case. Neither the NilClass nor the object nil have a method content.
Workaround:
doc.css('div.vsc').each do |element|
next unless elem = element.at_css("h3.r a.l")
puts elem.content
end

Resources