Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Range#cover? on begin-less ranges and non-integer values. #3810

Merged
merged 1 commit into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ New features:

Bug fixes:

* Fix `Range#cover?` on begin-less ranges and non-integer values (@nirvdrum, @rwstauner).

Compatibility:

Expand Down
15 changes: 13 additions & 2 deletions spec/ruby/core/range/shared/cover_and_include.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,26 @@
end

it "returns true if other is an element of self for endless ranges" do
eval("(1..)").send(@method, 2.4).should == true
eval("(0.5...)").send(@method, 2.4).should == true
(1..).send(@method, 2.4).should == true
(0.5...).send(@method, 2.4).should == true
end

it "returns true if other is an element of self for beginless ranges" do
(..10).send(@method, 2.4).should == true
(...10.5).send(@method, 2.4).should == true
end

it "returns false if values are not comparable" do
(1..10).send(@method, nil).should == false
(1...10).send(@method, nil).should == false

(..10).send(@method, nil).should == false
(...10).send(@method, nil).should == false

(1..).send(@method, nil).should == false
(1...).send(@method, nil).should == false
end

it "compares values using <=>" do
rng = (1..5)
m = mock("int")
Expand Down
4 changes: 3 additions & 1 deletion src/main/ruby/truffleruby/core/truffle/range_operations.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,15 @@ def self.cover?(range, value)
if !Primitive.nil?(range.begin)
# MRI uses <=> to compare, so must we.
cmp = (range.begin <=> value)
return false unless cmp
return false if Primitive.nil?(cmp)
return false if Comparable.compare_int(cmp) > 0
end

# Check upper bound.
if !Primitive.nil?(range.end)
cmp = (value <=> range.end)
return false if Primitive.nil?(cmp)

if range.exclude_end?
return false if Comparable.compare_int(cmp) >= 0
else
Expand Down
Loading