每个Ruby未定义的方法

时间:2016-02-09 19:25:19

标签: ruby-on-rails ruby json

我是ruby语言的新手,我现在试着学习它。 我有一个方法为Company

的课程find_applicants
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:in find_applicants': undefined method每个'为nil:NilClass(NoMethodError)       来自main.rb:11:在''

请帮助

1 个答案:

答案 0 :(得分:0)

看起来您传递的是作业所在的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变量