Project

General

Profile

Actions

Feature #14197

open

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

Added by znz (Kazuhiro NISHIYAMA) about 6 years ago. Updated almost 4 years ago.

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

Description

#all?, #any?, #none?, and #one? accept pattern arguments since 2.5.0.

#grep, and #grep_v have such feature, but it is hard for me to remember them since I would be thinking of using #select, or #reject, and #select and #reject don't have such feature.

I want to write

collection.reject(/re/)

instead of

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

or

collection.grep_v(/re/)

Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #11286: [PATCH] Add case equality arity to Enumerable's sequence predicates.Closedmatz (Yukihiro Matsumoto)Actions
Actions #1

Updated by znz (Kazuhiro NISHIYAMA) about 6 years ago

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

Updated by shevegen (Robert A. Heiler) about 6 years 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. :)

Actions #3

Updated by naruse (Yui NARUSE) about 6 years ago

  • Target version deleted (2.6)

Updated by inopinatus (Joshua GOODALL) about 6 years 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.

Updated by xavriley (Xavier Riley) almost 6 years 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

Updated by matz (Yukihiro Matsumoto) almost 6 years 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.

Updated by byroot (Jean Boussier) over 5 years 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.

Actions #8

Updated by sawa (Tsuyoshi Sawada) almost 4 years ago

  • Description updated (diff)
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0