[英]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.