[ruby-core:120921] [Ruby master Bug#21124] Enumerable#find called without a block returns Enumerator without size
From:
"mame (Yusuke Endoh) via ruby-core" <ruby-core@...>
Date:
2025-02-09 08:48:23 UTC
List:
ruby-core #120921
Issue #21124 has been updated by mame (Yusuke Endoh).
I really don't understand what the "size" of the Enumerator returned by `find` means, but I guess it returns `nil`, which means "unknown size", because the number of calls depends on the return value of the block.
```ruby
i = 0; [1, 2, 3].find.each {|x| i += 1; true }; p i #=> 1
i = 0; [1, 2, 3].find.each {|x| i += 1; false }; p i #=> 3
```
Why did you expect 3?
----------------------------------------
Bug #21124: Enumerable#find called without a block returns Enumerator without size
https://bugs.ruby-lang.org/issues/21124#change-111804
* Author: andrykonchin (Andrew Konchin)
* Status: Open
* ruby -v: 3.4.1
* Backport: 3.1: UNKNOWN, 3.2: UNKNOWN, 3.3: UNKNOWN, 3.4: UNKNOWN
----------------------------------------
When a collection's size is known then enumerator that iterates it usually has size (that's `#size` returns a collection size).
But an enumerator returned by `Enumerable#find` called without a block doesn't have size:
```ruby
[1, 2, 3].find.size # => nil
```
Expected result here is `3`.
Usually similar methods in the Enumerable module return enumerators with size (when these enumerators for instance iterate the collection itself):
```ruby
[1, 2, 3].each_entry.size # => 3
```
--
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/