diff options
author | Yusuke Endoh <mame@ruby-lang.org> | 2022-10-21 13:39:15 +0900 |
---|---|---|
committer | Yusuke Endoh <mame@ruby-lang.org> | 2022-10-21 16:35:46 +0900 |
commit | e026368061c56bc925aff58910a4b02f18b78c70 (patch) | |
tree | d4934806d3d9fa62957121d2e7f8dfacc050439b | |
parent | e72c5044cec4bb881300c3a4ade839d0882ec9db (diff) | |
download | ruby-e026368061c56bc925aff58910a4b02f18b78c70.tar.gz |
Range#size returns nil for (.."a") and (nil..)
Fixes [Bug #18983]
-rw-r--r-- | range.c | 4 | ||||
-rw-r--r-- | spec/ruby/core/range/size_spec.rb | 27 | ||||
-rw-r--r-- | test/ruby/test_range.rb | 3 |
3 files changed, 28 insertions, 6 deletions
@@ -816,7 +816,9 @@ range_size(VALUE range) } } else if (NIL_P(b)) { - return DBL2NUM(HUGE_VAL); + if (rb_obj_is_kind_of(e, rb_cNumeric)) { + return DBL2NUM(HUGE_VAL); + } } return Qnil; diff --git a/spec/ruby/core/range/size_spec.rb b/spec/ruby/core/range/size_spec.rb index 5462a1a5e1..9b625c9963 100644 --- a/spec/ruby/core/range/size_spec.rb +++ b/spec/ruby/core/range/size_spec.rb @@ -34,11 +34,28 @@ describe "Range#size" do eval("([]...)").size.should == nil end - it 'returns Float::INFINITY for all beginless ranges' do - (..1).size.should == Float::INFINITY - (...0.5).size.should == Float::INFINITY - (..nil).size.should == Float::INFINITY - (...'o').size.should == Float::INFINITY + ruby_version_is ""..."3.2" do + it 'returns Float::INFINITY for all beginless ranges' do + (..1).size.should == Float::INFINITY + (...0.5).size.should == Float::INFINITY + (..nil).size.should == Float::INFINITY + (...'o').size.should == Float::INFINITY + end + end + + ruby_version_is "3.2" do + it 'returns Float::INFINITY for all beginless ranges if the start is numeric' do + (..1).size.should == Float::INFINITY + (...0.5).size.should == Float::INFINITY + end + + it 'returns nil for all beginless ranges if the start is numeric' do + (...'o').size.should == nil + end + + it 'returns nil if the start and the end is both nil' do + (nil..nil).size.should == nil + end end it "returns nil if first and last are not Numeric" do diff --git a/test/ruby/test_range.rb b/test/ruby/test_range.rb index 0df0a985ad..2c07cef96e 100644 --- a/test/ruby/test_range.rb +++ b/test/ruby/test_range.rb @@ -777,6 +777,9 @@ class TestRange < Test::Unit::TestCase assert_equal 5, (1.1...6).size assert_equal 42, (1..42).each.size assert_nil ("a"..."z").size + assert_nil ("a"...).size + assert_nil (..."z").size # [Bug #18983] + assert_nil (nil...nil).size # [Bug #18983] assert_equal Float::INFINITY, (1...).size assert_equal Float::INFINITY, (1.0...).size |