Ruby 的“查找”方法不适用于 returns 数组的代码块。得到一个 NilClass 错误

Ruby's `find` method won't work on code block that returns an array. Gets a NilClass error

我的问题涉及 Ruby 的 find 方法以及为什么它在一种情况下有效而在另一种情况下无效。注意:我在用 Ruby.

编写的 Cucumber 步骤定义中使用此方法

此代码块按照我希望的方式工作:

Then(/^I expect the bolt service children for CrossrefDepositsDaf to include only: IngestUpdateWorkflow$/) do
  @boltworkflowservice ||= BoltWorkflowService.new
  Watir::Wait.until(60) do
    @job_family = @boltworkflowservice.get_workflows_job_family(@job_id).json
    @job_family.find { |job| job['type'] == "IngestUpdateWorkflow" }
  end
  expect(@job_family.find{|job| job['type'] == "IngestUpdateWorkflow"}.nil?).to eq(false), "IngestUpdateWorkflow child not found"
end

但是我想重写这个代码块,去掉实例变量,减少一行代码。我尝试了以下方法:

Then(/^I expect the bolt service children for CrossrefDepositsDaf to include only: IngestUpdateWorkflow$/) do
  @boltworkflowservice ||= BoltWorkflowService.new
  Watir::Wait.until(60) do
    puts @boltworkflowservice.get_workflows_job_family(@job_id).json
    @boltworkflowservice.get_workflows_job_family(@job_id).json.find {|job| job['type'] == "IngestUpdateWorkflow"}
  end
  expect(@job_family.find{|job| job['type'] == "IngestUpdateWorkflow"}.nil?).to eq(false), "IngestUpdateWorkflow child not found"
end

我刚刚添加了 puts 以查看代码 return。根据 puts,它 return 是一个包含三个散列对象的数组,但我仍然遇到错误。这是我尝试 运行 第二个块时得到的结果:

[{"id"=>914295, "type"=>"CrossrefDepositsDaf", "subType"=>"", "parentId"=>0, "state"=>"SUCCESS", "subState"=>"SUCCESS", "dataPointer"=>"s3://sequoia-install/app-resources/test.cirrostratus.org/workflows/CrossrefDepositsDaf/2015/7/29/148c7ebc-3da4-4650-a22e-2e8962d448fc", "created"=>"2015-07-29 01:58:14", "metadata"=>[]}, {"id"=>914296, "type"=>"CrossRefDeposits", "subType"=>"BoltCatDaf", "parentId"=>914295, "state"=>"SUCCESS", "subState"=>"SUCCESS", "dataPointer"=>"s3://sequoia-install/app-resources/test.cirrostratus.org/workflows/CrossRefDeposits/2015/7/29/3a703736-d4a2-456e-baa9-7bf2c0f4a0e2", "created"=>"2015-07-29 01:58:17", "metadata"=>[]}, {"id"=>914297, "type"=>"IngestUpdateWorkflow", "subType"=>"CrossRefDeposits", "parentId"=>914296, "state"=>"SUCCESS", "subState"=>"CREATED", "dataPointer"=>"s3://sequoia-install/app-resources/test.cirrostratus.org/workflows/IngestUpdateWorkflow/2015/7/29/9282591b-f93c-49d6-b656-4afbd14156ef", "created"=>"2015-07-29 01:58:23", "metadata"=>[]}]

NoMethodError: undefined method `find' for nil:NilClass
./features/step_definitions/catdaf_integration_crossref_deposits_steps.rb:42:in `/^I expect the bolt service children for CrossrefDepositsDaf to include only: IngestUpdateWorkflow$/'
C:\Users\Dugan23\workspace\cloudy_clouds\cucumber\features\content_management\daf\integration\crossref_deposits.feature:11:in `And I expect the bolt service children for CrossrefDepositsDaf to include only: IngestUpdateWorkflow'
1 scenario (1 failed)
6 steps (1 failed, 5 passed)
9m30.221s

Process finished with exit code 1

有人可以向我解释为什么第二个代码块不起作用吗? puts 显示我应该为 find 方法提供一个数组,那么为什么它说我正在尝试 运行 nil:NilClass 上的 find 方法]。提前感谢您的帮助。

注意:当我向第一个代码块添加 puts 语句以打印出 @job_family 时,它 return 编辑了与第二个代码块完全相同的数组。这是否意味着在第二个代码块中 Ruby 正在尝试 运行 find 方法,然后再评估之前的代码?我试图通过如下添加括号来解决这个问题,但无济于事:

(@boltworkflowservice.get_workflows_job_family(@job_id).json).find {|job| job['type'] == "IngestUpdateWorkflow"}

在第二个版本中,你已经避免在块中使用@job_family,但你仍然在期望中引用它,除非你在其他地方定义它,否则它显然是 nil