[ruby-core:68931] [Ruby trunk - Feature #11049] Enumerable#grep_v (inversed grep)

From: bottiger@...
Date: 2015-04-19 20:30:32 UTC
List: ruby-core #68931
Issue #11049 has been updated by Harald B=C3=B6ttiger.


Recursive Madman wrote:
> How about implementing `Regexp#invert` instead?

As grep using the case statement you are missing a lot of cases that would =
be handy:

array.invert_grep(Array) # Select all elements that are not arrays.

----------------------------------------
Feature #11049: Enumerable#grep_v (inversed grep)
https://bugs.ruby-lang.org/issues/11049#change-52196

* Author: Shota Fukumori
* Status: Open
* Priority: Normal
* Assignee: Yukihiro Matsumoto
----------------------------------------
sometime I want to do `grep -v` like operation:

    %w(aaa bbb ccc).reject { |x| /b/ =3D=3D=3D x } #=3D> ["aaa", "ccc"]

We already have Enumerable#grep, so I propose to add Enumerable#grep_v.

    %w(aaa bbb ccc).grep(/b/) #=3D> ["bbb"]
    %w(aaa bbb ccc).grep_v(/b/) #=3D> ["aaa", "ccc"]

## Naming / Interface

This idea is mentioned at DevelopersMeeting20150408Japan by me. Matz has sa=
id "I don't disagree for the feature. So this remains naming (interface) pr=
oblem."

I'm not sure grep_v is the best name for this feature; feedback are welcome=
d.

### Ideas

- `grep_v(pattern)` (the first patch)
- `grep(pattern, inverse: true)`
- `grep!(pattern)`

---Files--------------------------------
grepv.patch (4.24 KB)


--=20
https://bugs.ruby-lang.org/

In This Thread

Prev Next