:a, "20th Apr, 2013" => :b,"Tomorrow" => :c,"5:00 PM" => :d,"09:25-6ren">
gpt4 book ai didi

ruby - 如何仅从哈希中提取时间项?

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

我有一个如下所示的哈希:

{"19th Apr, 2013" => :a, "20th Apr, 2013" => :b,"Tomorrow" => :c,"5:00 PM" => :d,"09:25 PM" => :e}

我想要输出:

作为 {"5:00 PM"=> :d,"09:25 PM"=> :e}

谁能帮我解决这个问题?

最佳答案

t = {"19th Apr, 2013" => :a, "20th Apr, 2013" => :b,"Tomorrow" => :c,"5:00 PM" => :d,"09:25 PM" => :e}

t.select { |k,v| Time.strptime(k,"%H:%M %P") rescue false }

#=> {"5:00 PM"=>:d, "09:25 PM"=>:e}

关于ruby - 如何仅从哈希中提取时间项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16046722/

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