gpt4 book ai didi

ruby - 无法为变量 : undefined method `[]' for nil:NilClass (NoMethodError) 赋值

转载 作者:太空宇宙 更新时间:2023-11-03 18:28:26 26 4
gpt4 key购买 nike

我完全被这个难住了。

我有以下代码:

puts block.at_xpath("*/img")["width"].to_i

但是当我把它改成

width = block.at_xpath("*/img")["width"].to_i

我收到这个错误:

NokogiriTUT.rb:70:in `blockProcessor': undefined method `[]' for nil:NilClass (NoMethodError)

当我将看跌期权放在那里时,它会返回预期值。

更新:

        def blockProcessor(block)
header = block.xpath('td[@class="default"]/*/span[@class="comhead"]')
array = header.text.split
if array[0] != nil #checks to make sure we aren't at the top of the parent list
### Date and Time ###
if array[2] == 'hours' || array[2] == 'minutes'
date = Time.now
else
days = (array[1].to_i * 24 * 60 * 60)
date = Time.now - days
end

##Get Comment##
comment = block.at_xpath('*/span[@class="comment"]')

hash = comment.text.hash
#puts hash

##Manage Parent Here##

width = block.at_xpath("*/img")["width"].to_i



prevlevel = @parent_array[@parent_array.length-1][1]
if width == 0 #has parents
parentURL = header.xpath('a[@href][3]').to_s
parentURL = parentURL[17..23]
parentURL = "http://news.ycombinator.com/item?id=#{parentURL}"
parentdoc = Nokogiri::HTML(open(parentURL))
a = parentdoc.at_xpath("//html/body/center/table/tr[3]/td/table/tr")
nodeparent = blockProcessor(a)
@parent_array = []
node = [hash, width, nodeparent] #id, level, parent
@parent_array.push node
elsif width > prevlevel
nodeparent = @parent_array[@parent_array.length-1][0]
node = [hash, width, nodeparent]
@parent_array.push node
elsif width == prevlevel
nodeparent = @parent_array[@parent_array.length-1][2]
node = [hash, width, nodeparent]
@parent_array.push node
elsif width < prevlevel
until prevlevel == w do
@parent_array.pop
prevlevel = @parent_array[@parent_array.length-1][1]
end
nodeparent = @parent_array[@parent_array.length-1][2]
node = [hash, width, nodeparent]
@parent_array.push node
end
puts "Author: #{array[0]} with hash #{hash} with parent: #{nodeparent}"

##Handles Any Parents of Existing Comments ##
return hash
end
end
end

这是它作用于的 block 。

 <tr>
<td><img src="http://ycombinator.com/images/s.gif" height="1" width="0"></td>
<td valign="top"><center>
<a id="up_3004849" href="vote?for=3004849&amp;dir=up&amp;whence=%2f%78%3f%66%6e%69%64%3d%34%6b%56%68%71%6f%52%4d%38%44"><img src="http://ycombinator.com/images/grayarrow.gif" border="0" vspace="3" hspace="2"></a><span id="down_3004849"></span>
</center></td>
<td class="default">
<div style="margin-top:2px; margin-bottom:-10px; "><span class="comhead"><a href="user?id=patio11">patio11</a> 12 days ago | <a href="item?id=3004849">link</a> | <a href="item?id=3004793">parent</a> | on: <a href="item?id=3004471">Ask HN: What % of your job interviewees pass FizzB...</a></span></div>
<br><span class="comment"><font color="#000000">Every time FizzBuzz problems come up among engineers, people race to solve them and post their answers, then compete to see who can write increasingly more nifty answers for a question which does not seek niftiness at all.<p>I'm all for intellectual gamesmanship, but these are our professional equivalent of a doctor being asked to identify the difference between blood and water. You can do it. <i>We know</i>. Demonstrating that you can do it is not the point of the exercise. We do it to have a cheap-to-administer test to exclude people-who-cannot-actually-program-despite-previous-job-titles from the expensive portions of the hiring process.</p></font></span><p><font size="1"><u><a href="reply?id=3004849&amp;whence=%2f%78%3f%66%6e%69%64%3d%34%6b%56%68%71%6f%52%4d%38%44">reply</a></u></font></p>
</td>
</tr>

最佳答案

您的基本问题是您不了解 XPath。 (您在那里有很好的伙伴关系;XPath 非常令人困惑。)您的选择器根本不匹配您认为它们匹配的内容。尤其是那个爆炸的

*/img

应该是

//img

或类似的东西。

现在,因为 xpath 选择器不匹配任何东西,这个 Ruby 语句的值

block.at_xpath("*/img")

为零。而且 nil 不支持 [],因此当您尝试对其调用 ["width"] 时,Ruby 会提示 undefined method [] for nil :NilClass 错误。

至于为什么只有当您将它分配给一个变量时它才会爆炸……是的,这实际上不是发生的事情。您可能还更改了其他内容。


现在,请允许我提出一些其他希望有建设性的代码批评:

  • 您的问题显然是为了让人难以回答而设计的。以后,请隔离有问题的代码,不要只粘贴您的整个家庭作业(或此屏幕抓取器的用途)。

  • 如果你把它做成一个单独的可运行的 Ruby 文件,我们可以在我们的计算机上逐字执行,那就更好了,例如:

.

require "nokogiri"
doc = Nokogiri.parse <<-HTML
<tr>
<td><img src="http://ycombinator.com/images/s.gif" height="1" width="0"></td>
<td valign="top"><center>
<a id="up_3004849" href="vote?for=3004849&amp;dir=up&amp;whence=%2f%78%3f%66%6e%69%64%3d%34%6b%56%68%71%6f%52%4d%38%44"><img src="http://ycombinator.com/images/grayarrow.gif" border="0" vspace="3" hspace="2"></a><span id="down_3004849"></span>
</center></td>
<td class="default">
<div style="margin-top:2px; margin-bottom:-10px; ">
<span class="comhead">
<a href="user?id=patio11">patio11</a> 12 days ago | <a href="item?id=3004849">link</a> | <a href="item?id=3004793">parent</a> | on: <a href="item?id=3004471">Ask HN: What % of your job interviewees pass FizzB...</a>
</span>
</div>
<br><span class="comment"><font color="#000000">Every time FizzBuzz problems come up among engineers, people race to solve them and post their answers, then compete to see who can write increasingly more nifty answers for a question which does not seek niftiness at all.<p>I'm all for intellectual gamesmanship, but these are our professional equivalent of a doctor being asked to identify the difference between blood and water. You can do it. <i>We know</i>. Demonstrating that you can do it is not the point of the exercise. We do it to have a cheap-to-administer test to exclude people-who-cannot-actually-program-despite-previous-job-titles from the expensive portions of the hiring process.</p></font></span><p><font size="1"><u><a href="reply?id=3004849&amp;whence=%2f%78%3f%66%6e%69%64%3d%34%6b%56%68%71%6f%52%4d%38%44">reply</a></u></font></p>
</td>
</tr>
HTML

width = doc.at_xpath("*/img")["width"].to_i

这样我们就可以用我们的计算机进行调试,而不仅仅是用我们的头脑。

  • 您现在正在编写 Ruby,而不是 Java,因此请遵守 Ruby 的 spacing and naming conventions :文件名是 snake_case,缩进是 2 个空格,没有制表符等。阅读格式错误的代码真的很困难——“错误”意味着“不标准”。

  • 在任何有描述性注释(### 日期和时间###)的地方都是提取方法的机会(def date_and_time(array)) 并使您的代码更清晰、更易于调试。

关于ruby - 无法为变量 : undefined method `[]' for nil:NilClass (NoMethodError) 赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7602252/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com