Skip to content

Commit e026368

Browse files
committed
Range#size returns nil for (.."a") and (nil..)
Fixes [Bug #18983]
1 parent e72c504 commit e026368

File tree

3 files changed

+28
-6
lines changed

3 files changed

+28
-6
lines changed

range.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,9 @@ range_size(VALUE range)
816816
}
817817
}
818818
else if (NIL_P(b)) {
819-
return DBL2NUM(HUGE_VAL);
819+
if (rb_obj_is_kind_of(e, rb_cNumeric)) {
820+
return DBL2NUM(HUGE_VAL);
821+
}
820822
}
821823

822824
return Qnil;

spec/ruby/core/range/size_spec.rb

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,28 @@
3434
eval("([]...)").size.should == nil
3535
end
3636

37-
it 'returns Float::INFINITY for all beginless ranges' do
38-
(..1).size.should == Float::INFINITY
39-
(...0.5).size.should == Float::INFINITY
40-
(..nil).size.should == Float::INFINITY
41-
(...'o').size.should == Float::INFINITY
37+
ruby_version_is ""..."3.2" do
38+
it 'returns Float::INFINITY for all beginless ranges' do
39+
(..1).size.should == Float::INFINITY
40+
(...0.5).size.should == Float::INFINITY
41+
(..nil).size.should == Float::INFINITY
42+
(...'o').size.should == Float::INFINITY
43+
end
44+
end
45+
46+
ruby_version_is "3.2" do
47+
it 'returns Float::INFINITY for all beginless ranges if the start is numeric' do
48+
(..1).size.should == Float::INFINITY
49+
(...0.5).size.should == Float::INFINITY
50+
end
51+
52+
it 'returns nil for all beginless ranges if the start is numeric' do
53+
(...'o').size.should == nil
54+
end
55+
56+
it 'returns nil if the start and the end is both nil' do
57+
(nil..nil).size.should == nil
58+
end
4259
end
4360

4461
it "returns nil if first and last are not Numeric" do

test/ruby/test_range.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,9 @@ def test_size
777777
assert_equal 5, (1.1...6).size
778778
assert_equal 42, (1..42).each.size
779779
assert_nil ("a"..."z").size
780+
assert_nil ("a"...).size
781+
assert_nil (..."z").size # [Bug #18983]
782+
assert_nil (nil...nil).size # [Bug #18983]
780783

781784
assert_equal Float::INFINITY, (1...).size
782785
assert_equal Float::INFINITY, (1.0...).size

0 commit comments

Comments
 (0)