Project

General

Profile

Actions

Feature #9602

closed

Logic with `Enumerable#grep`

Added by sawa (Tsuyoshi Sawada) almost 11 years ago. Updated about 5 years ago.

Status:
Feedback
Target version:
-
[ruby-core:96047]

Description

Enumerable#grep is useful to filter things:

[nil, {}, [], 1, :foo, "foo"].grep(String)
# => ["foo"]
  1. Often, the condition cannot be expressed as a single object on which === is applied, but as a disjunction over === applied to multiple objects. I would like Enumerable#grep to take arbitrary number of arguments, and when they are more than one, a logical disjunction applies, just as when there are multiple comma-separated objects after when in case condition:

     [nil, {}, [], 1, :foo, "foo"].grep(String, Symbol, Array)
     # => [[], :foo, "foo"]
    
  2. Also, it often happens that I want the negation of grep. Perhaps, Enumerable#grepv (grepv comes from grep -v) can be implemented as negation of Enumerable#grep, i.e., select elements for which === returns false on any of the arguments:

     [nil, {}, [], 1, :foo, "foo"].grepv(String, Symbol, Array)
     # => [nil, {}, 1]
    

Files

select-to-accept-args.patch (2.62 KB) select-to-accept-args.patch srawlins (Sam Rawlins), 03/07/2014 04:18 AM

Related issues 1 (0 open1 closed)

Related to Ruby master - Feature #11049: Enumerable#grep_v (inversed grep)Closedmatz (Yukihiro Matsumoto)Actions
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0