Project

General

Profile

Feature #14197

`Enumerable#{select,reject}` accept a pattern argument

Added by znz (Kazuhiro NISHIYAMA) about 1 month ago. Updated 7 days ago.

Status:
Open
Priority:
Normal
Assignee:
-
Target version:
-
[ruby-core:84333]

Description

#all?, #any?, #none?, and #one? accept a pattern argument since 2.5.0.
But #select, and #reject don't.

The features are exist as #grep, and #grep_v, but there are hard to remember for me when I use #select, or #reject.

So I want to write

collection.reject(/re/)

instead of

collection.reject {|item| /re/ =~ item }

nor

collection.grep_v(/re/)

Related issues

Related to Ruby trunk - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.Closed

History

#1 Updated by znz (Kazuhiro NISHIYAMA) about 1 month ago

  • Related to Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates. added

#2 [ruby-core:84379] Updated by shevegen (Robert A. Heiler) 29 days ago

I think this suggestion looks ok, unless I may have missed something.

The main difference Kazuhiro is suggesting, appears to be the shorter
notation via regex given to .reject() rather than use the (longer)
block variant.

Here is the link to #grep:

https://ruby-doc.org/core-2.1.0/Enumerable.html#method-i-grep

which shows one example:

c = IO.constants
c.grep(/SEEK/)         #=> [:SEEK_SET, :SEEK_CUR, :SEEK_END]

On a side note, while I have used .grep(), I have never used .grep_v()
and it is a bit mysterious to me what grep_v does from the name
alone. :)

#3 Updated by naruse (Yui NARUSE) 24 days ago

  • Target version deleted (2.6)

#4 [ruby-core:84840] Updated by inopinatus (Joshua GOODALL) 7 days ago

I have personally found it useful to implement Regexp#to_proc as in https://bugs.ruby-lang.org/issues/7883 which permits collection.reject(&/re/) and more besides.

Also available in: Atom PDF