Skip to content

Commit fd0a964

Browse files
transclaude
andcommitted
Restore Range#overlap? for Ruby < 3.3 (guarded)
Ruby 3.3 added a native Range#overlap?, and Facets dropped its own version in the 3.2 modernization -- which left 3.1/3.2 users without it. Bring the method back under a `method_defined?(:overlap?)` guard so it backfills on older Rubies without replacing the built-in on 3.3+. Wire it into range.rb and add a test. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 7152b23 commit fd0a964

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

‎lib/core/facets/range.rb‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require_relative 'range/combine.rb'
2+
require_relative 'range/overlap.rb'
23
#require_relative 'range/intersection.rb' # too new
34
#require_relative 'range/quantile.rb' # uncommon
45
#require_relative 'range/op_add.rb' # too new

‎lib/core/facets/range/overlap.rb‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Range
2+
3+
# Do two ranges overlap?
4+
#
5+
# (1..5).overlap?(3..8) #=> true
6+
# (1..5).overlap?(6..8) #=> false
7+
#
8+
# Ruby 3.3 added a native Range#overlap?, so only define Facets' version
9+
# on older Rubies that lack it -- this avoids replacing the built-in.
10+
#
11+
# CREDIT: Daniel Schierbeck, Brandon Keepers
12+
13+
unless method_defined?(:overlap?)
14+
def overlap?(other)
15+
include?(other.first) or other.include?(first)
16+
end
17+
end
18+
19+
end

‎test/core/range/test_overlap.rb‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
covers 'facets/range/overlap'
2+
3+
test_case Range do
4+
5+
method :overlap? do
6+
7+
test "true when two ranges share elements" do
8+
(1..5).overlap?(3..8).assert == true
9+
end
10+
11+
test "false when two ranges are disjoint" do
12+
(1..5).overlap?(6..8).assert == false
13+
end
14+
15+
test "true when ranges touch at a boundary" do
16+
(1..5).overlap?(5..9).assert == true
17+
end
18+
19+
test "true when one range contains the other" do
20+
(1..10).overlap?(3..5).assert == true
21+
end
22+
23+
end
24+
25+
end

0 commit comments

Comments
 (0)