Project

General

Profile

Actions

Bug #20980

closed

Range#size new TypeError vs semi-open ranges

Added by zverok (Victor Shepelev) about 1 month ago. Updated 27 days ago.

Status:
Closed
Assignee:
-
Target version:
-
ruby -v:
ruby 3.4.0dev (2024-12-24T14:40:12Z master 07e89bde46) +PRISM [x86_64-linux]
[ruby-core:120397]

Description

Since #18984, Range#size throws TypeError if the begin is not iterable (doesn't respond to #succ). It produces one small inconvenience compared to 3.3:

('a'..).size #=> nil
(..'a').size 
# 3.3: nil
# 3.4: can't iterate from NilClass (TypeError) -- well, probably makes sense

# BUT:
(3..).size #=> Infinity -- cool!
(..3).size 
# 3.3: Infinity -- reasonable?
# 3.4: can't iterate from NilClass (TypeError) -- worse?

I believe that while 3.4's behavior is explainable, 3.3's one has better DX.

(Obviously too late to change before the release, but maybe might be considered for some patch version or Ruby 3.5?..)

Updated by mame (Yusuke Endoh) 27 days ago

  • Status changed from Open to Closed

The current behavior is intended. In principle, #size should return how many times #each will call a given block. (It could return nil as unknown.) Since beginless range does not respond to #each, it is now natural for #size to raise an exception. @naruse (Yui NARUSE) said he will try improving the document.

Actions

Also available in: Atom PDF

Like0
Like0