(Ruby) How do you check whether a range contains a subset of another range?

后端 未结 10 1474
轻奢々
轻奢々 2021-02-05 11:06

If I have two ranges that overlap:

x = 1..10
y = 5..15

When I say:

puts x.include? y 

the output is:

10条回答
  •  温柔的废话
    2021-02-05 11:33

    If you're checking for overlap, then I'd just do

    (x.include? y.first) or (x.include? y.last)
    

    as one range will have to include at least one of the ends of the other. This is more intuitive to me than the accepted conjuction answer, though not quite as efficient as MarkusQ's limit comparison.

提交回复
热议问题