ruby-core@ruby-lang.org archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-core:120397] [Ruby master Bug#20980] Range#size new TypeError vs semi-open ranges
@ 2024-12-24 15:39 zverok (Victor Shepelev) via ruby-core
  2025-01-09 11:42 ` [ruby-core:120571] " mame (Yusuke Endoh) via ruby-core
  0 siblings, 1 reply; 2+ messages in thread
From: zverok (Victor Shepelev) via ruby-core @ 2024-12-24 15:39 UTC (permalink / raw)
  To: ruby-core; +Cc: zverok (Victor Shepelev)

Issue #20980 has been reported by zverok (Victor Shepelev).

----------------------------------------
Bug #20980: Range#size new TypeError vs semi-open ranges
https://bugs.ruby-lang.org/issues/20980

* Author: zverok (Victor Shepelev)
* Status: Open
* ruby -v: ruby 3.4.0dev (2024-12-24T14:40:12Z master 07e89bde46) +PRISM [x86_64-linux]
* Backport: 3.1: DONTNEED, 3.2: DONTNEED, 3.3: DONTNEED
----------------------------------------
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:

```ruby
('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?..)



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [ruby-core:120571] [Ruby master Bug#20980] Range#size new TypeError vs semi-open ranges
  2024-12-24 15:39 [ruby-core:120397] [Ruby master Bug#20980] Range#size new TypeError vs semi-open ranges zverok (Victor Shepelev) via ruby-core
@ 2025-01-09 11:42 ` mame (Yusuke Endoh) via ruby-core
  0 siblings, 0 replies; 2+ messages in thread
From: mame (Yusuke Endoh) via ruby-core @ 2025-01-09 11:42 UTC (permalink / raw)
  To: ruby-core; +Cc: mame (Yusuke Endoh)

Issue #20980 has been updated by mame (Yusuke Endoh).

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 said he will try improving the document.

----------------------------------------
Bug #20980: Range#size new TypeError vs semi-open ranges
https://bugs.ruby-lang.org/issues/20980#change-111395

* Author: zverok (Victor Shepelev)
* Status: Closed
* ruby -v: ruby 3.4.0dev (2024-12-24T14:40:12Z master 07e89bde46) +PRISM [x86_64-linux]
* Backport: 3.1: DONTNEED, 3.2: DONTNEED, 3.3: DONTNEED
----------------------------------------
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:

```ruby
('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?..)



-- 
https://bugs.ruby-lang.org/
 ______________________________________________
 ruby-core mailing list -- ruby-core@ml.ruby-lang.org
 To unsubscribe send an email to ruby-core-leave@ml.ruby-lang.org
 ruby-core info -- https://ml.ruby-lang.org/mailman3/lists/ruby-core.ml.ruby-lang.org/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2025-01-09 11:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-12-24 15:39 [ruby-core:120397] [Ruby master Bug#20980] Range#size new TypeError vs semi-open ranges zverok (Victor Shepelev) via ruby-core
2025-01-09 11:42 ` [ruby-core:120571] " mame (Yusuke Endoh) via ruby-core

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).