親愛的計算器社區,紅寶石。破解寶石。 - 在'<main> ':未定義的方法`[]' 的零:NilClass(NoMethodError) -
初學者的問題: 爲什麼我得到下面的錯誤?
scraper_sample_2.rb:7:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)
>Exit code: 1
這裏是我的代碼(從Ruby的介紹指南複製):
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