Project

General

Profile

Actions

Feature #16833

open

Add Enumerable#empty?

Added by f3ndot (Justin Bull) over 4 years ago. Updated 8 months ago.

Status:
Assigned
Target version:
-
[ruby-core:98150]

Description

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.

 # 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:

[].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) add-enumerable-empty.patch f3ndot (Justin Bull), 05/06/2020 02:30 PM
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0