我在第13行出现错误。它说`<

问题描述 投票:0回答:2

这是代码:

  def self.scrape_shoe
    @doc.css("div.product-card__body").each do |nike|
      name = nike.css("div.product-card__title").text.strip
      price = nike.css("div.product-card__price").text.strip
      shoes = self.new
      @all << shoes #having error here
    end 
  end 

这是错误:

Traceback (most recent call last):
        7: from bin/new_nikes:7:in `<main>'
        6: from /home/merkonical/new_nikes/lib/new_nikes/cli.rb:17:in `call'
        5: from /home/merkonical/new_nikes/lib/new_nikes/cli.rb:11:in `list_price'
        4: from /home/merkonical/new_nikes/lib/new_nikes/scraper.rb:9:in `scrape_shoe'
        3: from /home/merkonical/.rvm/gems/ruby-2.6.1/gems/nokogiri-1.10.4/lib/nokogiri/xml/node_set.rb:237:in `each'
        2: from /home/merkonical/.rvm/gems/ruby-2.6.1/gems/nokogiri-1.10.4/lib/nokogiri/xml/node_set.rb:237:in `upto'
        1: from /home/merkonical/.rvm/gems/ruby-2.6.1/gems/nokogiri-1.10.4/lib/nokogiri/xml/node_set.rb:238:in `block in each'
/home/merkonical/new_nikes/lib/new_nikes/scraper.rb:13:in `block in scrape_shoe': undefined method `<<' for nil:NilClass (NoMethodError)

我可以为此做哪些修复?我正在研究Ruby

ruby command-line-interface
2个回答
1
投票

我可以为此做哪些修复?

@allnilnil没有名为<<的方法。确保@all不是nil


0
投票

由于将shoes对象推入未定义的实例变量而出现错误。

定义有效的@all

def self.scrape_shoe 
  @doc.css("div.product-card__body").each do |nike|
      @all = []
      name = nike.css("div.product-card__title").text.strip
      price = nike.css("div.product-card__price").text.strip
      shoes = self.new
      @all << shoes #having error here
    end 
  end

希望这对您有帮助

© www.soinside.com 2019 - 2024. All rights reserved.