[ruby-core:98155] [Ruby master Feature#16833] Add Enumerator#empty?
From:
shevegen@...
Date:
2020-05-06 19:55:43 UTC
List:
ruby-core #98155
Issue #16833 has been updated by shevegen (Robert A. Heiler).
sawa wrote:
> If that was what you meant, then that is not the case. Array mixes-in
> Enumerable, not Enumerator.
Happens to me as well a lot - I always mix these two up. May be because
the name is so similar. :)
To the suggestion itself: I have no particular preference either way so
I won't really comment on the proposal itself.
----------------------------------------
Feature #16833: Add Enumerator#empty?
https://bugs.ruby-lang.org/issues/16833#change-85394
* Author: f3ndot (Justin Bull)
* Status: Open
* Priority: Normal
----------------------------------------
It was surprising to me that Enumerator, something mixed into Array, does not include `#empty?`. I think it is reasonable to assume people may have to guard iterating and other logic based on the emptiness of an enumerator, such was my case.
``` ruby
# pretend there's convoluted enumerator logic to produce this structure
table_rows = [{ data: ['First', 'Second', 'Third'], config: {} }, { data: [4, 5, 6], config: { color: 'red' } }].to_enum
return if table_rows.empty?
table_header = table_rows.first[:data] # requires an empty guard
# ...
```
I propose that it simply behaves as `#take(1).to_a.empty?` instead of aliasing to something like `#none?` because of falsey elements or `#size == 0` because of potential `nil` returns:
```ruby
[].to_enum.empty? # => true
[false].to_enum.empty? # => false
[nil].to_enum.empty? # => false
[0].to_enum.empty? # => false
[1, 2, 3].to_enum.empty? # => false
```
---Files--------------------------------
add-enumerable-empty.patch (1.99 KB)
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request@ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>