Skip to content

Commit ee179eb

Browse files
committed
[GR-19220] Return nil for beginless ranges where the end is not numeric
PullRequest: truffleruby/3771
2 parents 68f5b26 + 3446588 commit ee179eb

File tree

5 files changed

+8
-3
lines changed

5 files changed

+8
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ Compatibility:
1313
* Add `Process.argv0` method (@andrykonchin).
1414
* Add support for array pattern matching. This is opt-in via `--pattern-matching` since pattern matching is not fully supported yet. (#2683, @razetime).
1515
* Fix `Array#[]` with `ArithmeticSequence` argument when step is negative (@itarato).
16+
* Fix `Range#size` and return `nil` for beginningless Range when end isn't Numeric (@rwstauner).
1617

1718
Performance:
1819

spec/ruby/core/range/size_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,12 @@
4444
end
4545

4646
ruby_version_is "3.2" do
47-
it 'returns Float::INFINITY for all beginless ranges if the start is numeric' do
47+
it 'returns Float::INFINITY for all beginless ranges if the end is numeric' do
4848
(..1).size.should == Float::INFINITY
4949
(...0.5).size.should == Float::INFINITY
5050
end
5151

52-
it 'returns nil for all beginless ranges if the start is numeric' do
52+
it 'returns nil for all beginless ranges if the end is not numeric' do
5353
(...'o').size.should == nil
5454
end
5555

spec/tags/core/range/size_tags.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
fails:Range#size returns Float::INFINITY for all beginless ranges

spec/truffleruby.next-specs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ spec/ruby/core/array/slice_spec.rb
1313
spec/ruby/core/array/element_reference_spec.rb
1414

1515
spec/ruby/core/hash/shift_spec.rb
16+
spec/ruby/core/range/size_spec.rb

src/main/ruby/truffleruby/core/range.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,9 @@ def cover?(value)
509509
end
510510

511511
def size
512-
return Float::INFINITY if Primitive.nil? self.begin
512+
if Primitive.nil? self.begin
513+
return Primitive.is_a?(self.end, Numeric) ? Float::INFINITY : nil
514+
end
513515
return nil unless Primitive.is_a?(self.begin, Numeric)
514516
return Float::INFINITY if Primitive.nil? self.end
515517

0 commit comments

Comments
 (0)