Project

General

Profile

Actions

Bug #18983

closed

Range#size for beginless Range is not nil.

Added by masasakano (Masa Sakano) over 1 year ago. Updated over 1 year ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.1.2p20 (2022-04-12 revision 4491bb740a) [x86_64-darwin20]
[ruby-core:109770]

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

bug-20220829-beginless_range.rb (1.24 KB) bug-20220829-beginless_range.rb Ruby executable code to demonstrate the point re beginless Range#size. masasakano (Masa Sakano), 08/29/2022 02:05 PM
6378.patch (1.37 KB) 6378.patch thiagofm (Thiago Massa), 09/14/2022 09:58 PM

Related issues 1 (0 open1 closed)

Related to Ruby master - Bug #14699: Subtle behaviors with endless rangeClosedActions

Updated by thiagofm (Thiago Massa) over 1 year ago

Nevermind. Build failed.

Updated by jeremyevans0 (Jeremy Evans) over 1 year 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.

Actions #4

Updated by mame (Yusuke Endoh) over 1 year ago

  • Related to Bug #14699: Subtle behaviors with endless range added

Updated by matz (Yukihiro Matsumoto) over 1 year 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) over 1 year ago

Oops, I missed the attached patch. Sorry

Actions #8

Updated by mame (Yusuke Endoh) over 1 year ago

  • Status changed from Open to Closed

Applied in changeset git|e026368061c56bc925aff58910a4b02f18b78c70.


Range#size returns nil for (.."a") and (nil..)

Fixes [Bug #18983]

Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0