简体   繁体   English

查找第一个具有nil值的哈希键

[英]Find first hash key with nil value

def job_ids
 {
   job_id:   r.job_id,
   job_2_id: r.job_2_id,
   job_3_id: r.job_3_id
 }
end

def available_job_id
   job_ids.find { |_k, v| v.nil? }[0].to_s
end

im checking which key has the first value of nil ,and then i convert it to string. 我检查哪个键的第一个值为nil ,然后将其转换为字符串。

what's the best way to do it? 最好的方法是什么?

This seems to be dirty? 这好像很脏吗?

job_ids.find { |_k, v| v.nil? }[0].to_s

UPDATE: here's the desired output so far... 更新:这是到目前为止所需的输出...

[79] pry(main)> job_ids.find{|k, v| v.nil?}[0].to_s
=> "job_2_id"

尝试这个

job_ids.key(nil).to_s

You can just use key 你可以用key

job_ids.key(nil).to_s
#=> "job_2_id"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM