0

我有这个哈希

- "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:我使用伪代码进行日期比较只是为了使这段代码更具可读性:)

4

1 回答 1

2

尝试这个

programs.select do |p| 
  p.scheduling.any? do |ps|
    ps.start >= "7pm" && ps.stop <= "9pm"
  end
end
于 2013-01-28T21:36:26.353 回答