选择依次为二维哈希

时间:2013-01-28 21:27:16

标签: ruby

我有这个哈希

- "title" : "The Today Show",
- "category; "Show",
- "channel-name": "CNBC",
- "scheduling" => 
       { "start" : "7am", "stop" : "9am"},
       { "start" : "10am", "stop" : "11am"},
       { "start" : "11am", "stop": "12am"}
- "title" : "How I met your mother",
- "category; "Show",
- "channel-name": "CBS",
- "scheduling" => 
       { "start" : "7pm", "stop" : "9pm"},
       { "start" : "10pm", "stop" : "12pm"},
       { "start" : "11am", "stop": "12am"}

我只需要“选择”只有至少有一个时间表的节目“7pm” - “9pm”

我尝试了这个,但它无法正常工作

programs.select_by{|p| 
      p.scheduling.each{|ps|
         ps.start <= "7pm" && ps.stop <= "9pm"
   }
}

PS:我使用伪代码进行日期比较,只是为了让这段代码更具可读性:)

1 个答案:

答案 0 :(得分:2)

试试这个

programs.select do |p| 
  p.scheduling.any? do |ps|
    ps.start >= "7pm" && ps.stop <= "9pm"
  end
end