Bug #18983
closedRange#size for beginless Range is not nil.
Description
Range#size should, according to Official docs,
Returns the count of elements in self if both begin and end values are numeric; otherwise, returns nil
but it doesn't with beginless non-Numeric Ranges (in Ruby-3.1.2 or earlier).
(..?a).size      # => Infinity
(nil..nil).size  # => Infinity
whereas it certainly does with endless Ranges
(?a..).size      # => nil
Files
        
           Updated by thiagofm (Thiago Massa) about 3 years ago
          Updated by thiagofm (Thiago Massa) about 3 years ago
          
          
        
        
      
      - File 6378.patch 6378.patch added
        
           Updated by thiagofm (Thiago Massa) about 3 years ago
          Updated by thiagofm (Thiago Massa) about 3 years ago
          
          
        
        
      
      Nevermind. Build failed.
        
           Updated by jeremyevans0 (Jeremy Evans) about 3 years ago
          Updated by jeremyevans0 (Jeremy Evans) about 3 years ago
          
          
        
        
      
      Returning Infinity for numeric ranges bounded in either direction is expected (there are tests explicitly for it).
It does seem inconsistent that (..object).size returns Infinity and (object..).size returns nil for non-numeric objects.  I think returning nil in both cases makes sense.
        
           Updated by mame (Yusuke Endoh) about 3 years ago
          Updated by mame (Yusuke Endoh) about 3 years ago
          
          
        
        
      
      - Related to Bug #14699: Subtle behaviors with endless range added
        
           Updated by matz (Yukihiro Matsumoto) about 3 years ago
          Updated by matz (Yukihiro Matsumoto) about 3 years ago
          
          
        
        
      
      I agree with inconsistency; if edges were numeric, return the count (or Infinity), otherwise it should return nil.
nil .. nil can be considered as numeric-range both edge omitted, but at the moment I feel it should return nil as well.
Let us experiment how it works.
Matz.
        
           Updated by mame (Yusuke Endoh) about 3 years ago
          Updated by mame (Yusuke Endoh) about 3 years ago
          
          
        
        
      
      Oops, I missed the attached patch. Sorry
        
           Updated by mame (Yusuke Endoh) about 3 years ago
          Updated by mame (Yusuke Endoh) about 3 years ago
          
          
        
        
      
      - Status changed from Open to Closed
Applied in changeset git|e026368061c56bc925aff58910a4b02f18b78c70.
Range#size returns nil for (.."a") and (nil..)
Fixes [Bug #18983]