Project

General

Profile

Actions

Feature #17140

open

Merge Enumerable#grep(_v) with Enumerable#select/reject

Added by baweaver (Brandon Weaver) over 4 years ago. Updated almost 4 years ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:99824]

Description

In recent versions of Ruby we've gotten new behavior of some Enumerable methods like any?, all?, none?, one?, and others to support a single argument pattern that responds to ===. This is very powerful, and very useful.

Currently Enumerable has grep and grep_v which allow this as a way to filter lists.

These names require some understanding of Unix to be familiar with, but naming aside, I feel it may make sense to implement === pattern arguments in Enumerable#select and Enumerable#reject as with the above.

Proposed Syntax:

list_of_numbers.select(1..10)
words.reject({ 'and', 'the', 'of' })

I believe this would help with readability and would simplify syntax options by unifying on this standard.

My concern is that Enumerable#find already takes a single argument, ifnone, and may not be able to implement this behavior. I would be curious to see how many use ifnone but feel this would be more critically breaking to do.

Updated by duerst (Martin Dürst) over 4 years ago

I guess I understand the first example. Just to make sure:

[-3, 4, 0, 8.5, 20, 5].select(1..10) #=> [4, 8.5, 5]

But for the second example, I don't understand { 'and', 'the', 'of' }. Is that supposed to be an array, or what?

Updated by marcandre (Marc-Andre Lafortune) over 4 years ago

I imagine that Set['and', 'the', 'of'] was meant.

Updated by matz (Yukihiro Matsumoto) over 4 years ago

Enhancing select and reject seems a good idea.

Matz.

Updated by baweaver (Brandon Weaver) almost 4 years ago

Would this feature still be of interest?

Actions

Also available in: Atom PDF

Like1
Like0Like0Like0Like0Like0