ruby - 我在第 13 行收到一个错误。它说 `<<` 方法未定义。我该如何解决?我正在使用红宝石
问题描述
这是代码:
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)
我可以为此做哪些可能的修复?我正在研究红宝石
解决方案
我可以为此做哪些可能的修复?
@all
是nil
。nil
没有名为 的方法<<
。确定@all
不是nil
。
推荐阅读
- inno-setup - 如何检测安装 VC Redist 软件包后是否需要提示用户重新启动 PC
- node.js - 为什么我的函数在 Node.js 中执行后我的数组会被擦除?
- swift - 如何将纹理存储模式设置为“私有”以从“CVMetalTextureCacheCreateTextureFromImage”创建纹理?
- php - 如何使用 Laravel 查询全局范围
- c - 无法从 C 中的另一个文件中找到函数
- angular - Angular 8:无法读取未定义的属性“包含”
- spring - Spring Security:本地配置文件基于令牌,但开发配置文件默认为用户名密码
- typescript - 编译器仍然认为 "x" 在 "if (x !== undefined)" 块中是未定义的
- css - 用户登录时如何显示密码错误消息
- lua - 我无法在 wibox.container.scroll 内的小部件上捕捉鼠标信号