Bug #21124
closedEnumerable#find called without a block returns Enumerator without size
Description
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:
[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):
[1, 2, 3].each_entry.size # => 3
Updated by andrykonchin (Andrew Konchin) 5 days ago
- Subject changed from Enumerable#find called without block returns Enumerator without size to Enumerable#find called without a block returns Enumerator without size
Updated by mame (Yusuke Endoh) 4 days ago
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.
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?
Updated by nobu (Nobuyoshi Nakada) 3 days ago
- Status changed from Open to Feedback
Updated by andrykonchin (Andrew Konchin) 3 days ago
Yes, indeed. You are right. My bad.
I was confused by result of #to_a
called on such Enumerator:
[1, 2, 3].find.to_a # => [1, 2, 3]
Updated by mame (Yusuke Endoh) 2 days ago
- Status changed from Feedback to Rejected
Thanks for your confirmation. Closing.