Project

General

Profile

Feature #14197

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

Added by znz (Kazuhiro NISHIYAMA) 8 months ago. Updated 2 months 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) 8 months 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) 8 months 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) 8 months ago

  • Target version deleted (2.6)

#4 [ruby-core:84840] Updated by inopinatus (Joshua GOODALL) 7 months 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.

#5 [ruby-core:86247] Updated by xavriley (Xavier Riley) 5 months ago

This issue is also related https://bugs.ruby-lang.org/issues/9602

One interesting point there is the case where a pattern and a block are given:

%w{ant bear cat}.select(/bear/) {||x| x === "cat" }

This issue also applies to the implementation from the linked issue for #all?(pattern)but this is already implemented

>> %w{ant bear cat}.all?(String) {|x| x === Integer }
=> true

#6 [ruby-core:86248] Updated by matz (Yukihiro Matsumoto) 5 months ago

The point is that the phrase "hard to remember" in the OP is bit weak when we already have methods with the proposed behavior (grep and grep_v).

Matz.

#7 [ruby-core:87518] Updated by byroot (Jean Boussier) 2 months ago

The point is that the phrase "hard to remember" in the OP is bit weak when we already have methods with the proposed behavior (grep and grep_v).

Indeed. However I think there is a case to be made for consistency.

If this works:

%(foo bar).all?(String)

I do expect this to work as well.

%(foo bar).reject(String)

It is true that then select would end up being an alias of grep and reject an alias of grep_v, but I actually see it as a good thing.

Also available in: Atom PDF