How to find if range is contained in an array of ranges?

前端 未结 3 720
遥遥无期
遥遥无期 2020-12-17 05:35

Example

business_hours[\'monday\'] = [800..1200, 1300..1700]
business_hours[\'tuesday\'] = [900..1100, 1300..1700]

...

I then have

3条回答
  •  春和景丽
    2020-12-17 06:03

    To answer your question's title, find if a range of arrays contains a range:

    ary = [800..1200, 1300..1700]
    
    test = 800..830
    p ary.any? {|rng| rng.include?(test.first) and rng.include?(test.last)}
    # => true
    
    test = 1245..1330
    p ary.any? {|rng| rng.include?(test.first) and rng.include?(test.last)}
    # => false
    

    which could be written as

    class Range
      def include_range?(r)
        self.include?(r.first) and self.include?(r.last)
      end
    end
    

提交回复
热议问题