【发布时间】:2018-05-16 02:40:53
【问题描述】:
我试图仅在键 escalation_policies 具有值时才设置键 self 的值。这是一个没有升级策略的计划示例,因此我不想要键 self
{"schedules"=>
[{"id"=>"0000000",
"type"=>"schedule",
"summary"=>"-PROD-",
"self"=>"https://api.pagerduty.com/schedules/",
"html_url"=>"https://pagerduty.com/schedules/",
"name"=>"-PROD-",
"time_zone"=>"America/",
"description"=>nil,
"privilege"=>nil,
"users"=>
[{"id"=>"0000000",
"type"=>"user_reference",
"summary"=>"DOo Kkkk",
"self"=>"https://api.pagerduty.com/users/",
"html_url"=>"https://target.pagerduty.com/users/"}],
"escalation_policies"=>[],
"teams"=>[]},
}
我克服这一挑战的代码是:
somefile = File.open("Schedule_Api.txt", "a+")
jdoc.fetch("schedules").each do |schedule|
somefile.puts schedule["self"] if schedule["escalation_policies"].exists? == true
end
somefile.close
这个变量jdoc 是网站的卷曲。这就是我从中得到的结果是undefined methodexists?对于 []:Array (NoMethodError). Is there another alternative to this then the methodexists?`。任何帮助都会有所帮助!谢谢!
【问题讨论】:
-
我建议改写“仅当密钥 escalation_policies 具有值时”。尽管在更高的非技术层面上,空数组表示没有值,但这可能会让其他人感到困惑,就像对我一样。最好说“仅当键 escalation_policies 的哈希值是一个不为空的数组时”(或者其大小 > 0)。
-
会的!谢谢!