[英]Ruby undefined method each
我是红宝石语言的新手,我现在尝试学习它。 我有一个使用find_applicants
方法的班级Company
class Company
attr_accessor :jobs
## TODO: This method should update the `jobs` property to an array of instances of
## class `Job`
def initialize(jobs)
# Load the json file and loop over the jobs to create an array of instance of `Job`
# Assign the `jobs` instance variable.
@jobs = jobs
end
## TODO: Impelement this method to return applicants from all jobs with a
## tag matching this keyword
def find_applicants(keyword)
# Use the `jobs` instance variable.
applicants = []
@jobs.each do |job|
job.applicants.each do |applicant|
applicant.tags.each do |tag|
if keyword.eql? tag
# ...
end
end
end
end
end
和main.rb
require './src/company.rb'
require './src/applicant.rb'
require './src/job.rb'
require 'json'
company = Company.new('data/boundless.json')
applicants = company.find_applicants('google')
puts applicants
当编译这个我有这个错误
/users/user/Desktop/BoundlessCaseStudy/src/company.rb:34:在
find_applicants': undefined method
main.rb:11:in中的nil:NilClass(NoMethodError)的每个find_applicants': undefined method
请帮忙
看起来您正在传递作业所在的json文件的名称,而不是作业数组
company = Company.new('data/boundless.json')
但是您没有编写可解析该文件的que代码,因为注释正确地表明您应该
# Load the json file and loop over the jobs to create an array of instance of `Job`,
因此,变量@jobs正在接收字符串,您将收到此错误:
NoMethodError: undefined method `each' for "data/boundless.json":String
编写代码以解析文件并正确设置@jobs变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.