From: "akr (Akira Tanaka) via ruby-core" Date: 2023-09-18T13:55:02+00:00 Subject: [ruby-core:114800] [Ruby master Feature#19839] Need a method to check if two ranges overlap Issue #19839 has been updated by akr (Akira Tanaka). I found another corner case. ``` % ./ruby -e 'r = (...-Float::INFINITY); p r.overlap?(r)' true % ./ruby -e 'r = (...[]); p r.overlap?(r)' true % ./ruby -e 'r = (...""); p r.overlap?(r)' true % ./ruby -e 'r = (...true); p r.overlap?(r)' true % ./ruby -e 'r = (...false); p r.overlap?(r)' true % ./ruby -e 'r = (...Object.new); p r.overlap?(r)' true % ./ruby -v ruby 3.3.0dev (2023-09-16T22:27:04Z master cd67c0d204) [x86_64-linux] ``` They are `(...MINIMUM)` where MINIMUM is a value that there is no value less than that. `(...MINIMUM)` is empty because (1) it doesn't contain MINIMUM itself because `...` is used (exclude_end is true) and (2) it doesn't contain other values because there is no value less than MINIMUM. But `(...MINIMUM).overlap?(...MINIMUM)` returns true as shown above. It means Ruby says there is a value contained in (...MINIMUM). It is invalid. Note that true, false, and `Object.new` has `<=>` method defined by Kernel. It determines self is equal to itself and other objects are not comparable. Thus, they can be considered as minimum values (and maximum values). I think this problem is difficult to solve. We have no clean way to detect minimum values. Some options: (1) introduce some protocol to detect minimum values such as `minimum?` method. (2) hard code minimum values of builtin classes in `Range#overlap?` and use `minimum?` for user-defined classes. (3) document this limitation. ---------------------------------------- Feature #19839: Need a method to check if two ranges overlap https://bugs.ruby-lang.org/issues/19839#change-104645 * Author: shouichi (Shouichi Kamiya) * Status: Closed * Priority: Normal ---------------------------------------- It would be convenient to have a method that checks if two ranges overlap. For example, ``` (0..10).overlap?(5..15) #=> true (0..10).overlap?(20..30) #=> false ``` -- https://bugs.ruby-lang.org/ ______________________________________________ ruby-core mailing list -- ruby-core@ml.ruby-lang.org To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org ruby-core info -- https://ml.ruby-lang.org/mailman3/postorius/lists/ruby-core.ml.ruby-lang.org/