Feature #11049

Enumerable#grep_v (inversed grep)

Shota Fukumori8ヶ月前に追加. 6ヶ月前に更新.

[ruby-core:<unknown>]
ステータス:Closed
優先度:Normal
担当者:Yukihiro Matsumoto

説明

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)

grepv.patch Magnifier (4.24 KB) Shota Fukumori, 2015/04/08 10:23


関連するチケット

関連している Ruby trunk - Feature #5588: add negation flag (v) to Regexp Feedback 2011/11/08
関連している Ruby trunk - Feature #9602: Logic with `Enumerable#grep` Feedback 2014/03/06

履歴

#1 Recursive Madman8ヶ月前に更新

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"]

#2 Benoit Daloze8ヶ月前に更新

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.

#3 Benoit Daloze8ヶ月前に更新

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

#4 Recursive Madman8ヶ月前に更新

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?

#5 Akira Tanaka8ヶ月前に更新

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) #=> []

#6 Nobuyoshi Nakada8ヶ月前に更新

invert looks same as my proposal, Regexp#!.

#7 Tsuyoshi Sawada8ヶ月前に更新

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.

#8 Shota Fukumori8ヶ月前に更新

  • 担当者Yukihiro Matsumoto にセット

I didn't know/hear that.

@matz what do you think for now?

#9 Akira Tanaka7ヶ月前に更新

  • 関連している Feature #5588: add negation flag (v) to Regexp を追加

#10 Akira Tanaka7ヶ月前に更新

  • 関連している Feature #9602: Logic with `Enumerable#grep` を追加

#11 Harald Böttiger7ヶ月前に更新

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.

#12 Yukihiro Matsumoto6ヶ月前に更新

grep_v seems OK. Accepted.

Matz.

#13 Shota Fukumori6ヶ月前に更新

  • ステータスOpen から Closed に変更

Thanks, committed at r50491, r50492.

他の形式にエクスポート: Atom PDF