gpt4 book ai didi

ruby-on-rails - 检查哈希是否包含任何空值

转载 作者:数据小太阳 更新时间:2023-10-29 07:16:57 27 4
gpt4 key购买 nike

我有以下散列,我正在寻找一种简单的方法来检查散列中的所有内容是否为空。并非所有值都始终在散列中可见,因此有时散列没有 end_date/start_date 或其他任何内容。

test
=> {"0"=>
{"_destroy"=>"",
"START_DATE"=>"",
"END_DATE"=>"",
"EMPLOYER"=>"",
"JOB_TITEL"=>"",
"FUNCTIONAL_AREA"=>"",
"INDUSTRY"=>"",
"DESCRIPTION_TXT"=>"",
"COUNTRY"=>"",
"CITY"=>"",
"REGION"=>"",
"CONTRACT_TYPE"=>""},
"1"=>
{"_destroy"=>"",
"START_DATE"=>"",
"END_DATE"=>"",
"EMPLOYER"=>"",
"JOB_TITEL"=>"",
"FUNCTIONAL_AREA"=>"",
"INDUSTRY"=>"",
"DESCRIPTION_TXT"=>"",
"COUNTRY"=>"",
"CITY"=>"",
"REGION"=>"",
"CONTRACT_TYPE"=>""},
"2"=>
{"_destroy"=>"",
"START_DATE"=>"",
"END_DATE"=>"",
"EMPLOYER"=>"",
"JOB_TITEL"=>"",
"FUNCTIONAL_AREA"=>"",
"INDUSTRY"=>"",
"DESCRIPTION_TXT"=>"",
"COUNTRY"=>"",
"CITY"=>"",
"REGION"=>"",
"CONTRACT_TYPE"=>""}}

在伪代码中它会像这样

Start loop
-> check if current position contains an empties
=> if all is empty delete position
-> continue
end loop

在这个例子中,这意味着哈希在循环结束时将为空。

亲切的问候

最佳答案

使用Hash#delete_if :

test.delete_if { |i,h| h.all? { |k,v| v.empty? } }

关于ruby-on-rails - 检查哈希是否包含任何空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22181315/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com