Project

General

Profile

Actions

Feature #11049

closed

Enumerable#grep_v (inversed grep)

Added by sorah (Sorah Fukumori) over 9 years ago. Updated over 9 years ago.

Status:
Closed
Target version:
-
[ruby-core:<unknown>]

Description

sometime I want to do grep -v like operation:

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

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

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

Naming / Interface

This idea is mentioned at DevelopersMeeting20150408Japan by me. Matz has said "I don't disagree for the feature. So this remains naming (interface) problem."

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

Ideas

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

Files

grepv.patch (4.24 KB) grepv.patch sorah (Sorah Fukumori), 04/08/2015 10:23 AM

Related issues 3 (1 open2 closed)

Related to Ruby master - Feature #5588: add negation flag (v) to RegexpClosednaruse (Yui NARUSE)Actions
Related to Ruby master - Feature #9602: Logic with `Enumerable#grep`Feedbackmatz (Yukihiro Matsumoto)Actions
Related to Ruby master - Feature #8921: Allow select, reject, etc to accept a regexOpenActions
Actions #1

Updated by recursive-madman (Recursive Madman) over 9 years ago

How about implementing Regexp#invert instead?

  /b/.invert #=> /[^(?:b)]/

Like this for example:

class Regexp
  def invert
    self.class.new("[^(?:#{source})]")
  end
end

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

Actions #2

Updated by Eregon (Benoit Daloze) over 9 years ago

Recursive Madman wrote:

How about implementing Regexp#invert instead?

Like this for example:

class Regexp
  def invert
    self.class.new("[^(?:#{source})]")
  end
end

That only works for some regexps, I doubt there is a proper and well defined inverted regexp for any Regexp.

Actions #3

Updated by Eregon (Benoit Daloze) over 9 years ago

grep(pattern, inverse: true) seems the least surprising to me.
But of course it does not buy much compared to #reject.

Actions #4

Updated by recursive-madman (Recursive Madman) over 9 years ago

Benoit Daloze wrote:

That only works for some regexps, I doubt there is a proper and well defined inverted regexp for any Regexp.

Could you provide an example that doesn't work?

Actions #5

Updated by akr (Akira Tanaka) over 9 years ago

Recursive Madman wrote:

Benoit Daloze wrote:

That only works for some regexps, I doubt there is a proper and well defined inverted regexp for any Regexp.

Could you provide an example that doesn't work?

class Regexp
  def invert
    self.class.new("[^(?:#{source})]")
  end
end

%w(aaa bbb ccc).grep(/abc/) #=> []
%w(aaa bbb ccc).grep(/abc/.invert) #=> []
Actions #6

Updated by nobu (Nobuyoshi Nakada) over 9 years ago

invert looks same as my proposal, Regexp#!.

Actions #7

Updated by sawa (Tsuyoshi Sawada) over 9 years ago

How is this proposal different from #9602? I thought that Matz had already suggested to modify select, and Sam Rawlins had implemented select and reject.

Actions #8

Updated by sorah (Sorah Fukumori) over 9 years ago

  • Assignee set to matz (Yukihiro Matsumoto)

I didn't know/hear that.

@matz (Yukihiro Matsumoto) what do you think for now?

Actions #9

Updated by akr (Akira Tanaka) over 9 years ago

  • Related to Feature #5588: add negation flag (v) to Regexp added
Actions #10

Updated by akr (Akira Tanaka) over 9 years ago

Actions #11

Updated by haraldb (Harald Böttiger) over 9 years ago

Recursive Madman wrote:

How about implementing Regexp#invert instead?

As grep using the case statement this would be ignoring a lot of cases that would be handy:

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

Updated by matz (Yukihiro Matsumoto) over 9 years ago

grep_v seems OK. Accepted.

Matz.

Updated by sorah (Sorah Fukumori) over 9 years ago

  • Status changed from Open to Closed

Thanks, committed at r50491, r50492.

Actions #14

Updated by shyouhei (Shyouhei Urabe) over 8 years ago

  • Related to Feature #8921: Allow select, reject, etc to accept a regex added
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0Like0