From: sam.rawlins@... Date: 2014-03-07T04:24:31+00:00 Subject: [ruby-core:61347] [ruby-trunk - Feature #9602] Logic with `Enumerable#grep` Issue #9602 has been updated by Sam Rawlins. File select-to-accept-args.patch added +1 to Matz's idea. I think this would work like `Enumerable#count`, taking `*args` _or_ a block, but not both: %w{foo bar baz}.select #=> an Enumerator %w{foo bar baz}.select {|e| e['b']} #=> an Array ["bar", "baz"] %w{foo bar baz}.select(/b/) #=> an Array ["bar", "baz"] %w{foo bar baz}.select(/f/, /z/) #=> an Array ["foo", "baz"] %w{foo bar baz}.select(/b/) {|e| e['f']} # warns "given block not used", returns ["bar", "baz"] I have a short patch for Enumerable#select [on GitHub](https://github.com/srawlins/ruby/compare/select-to-accept-args), and attached. This idea should be extended to Enumerable#reject, and Array's #select and #reject, and probably Array's #select! and #reject!, and maybe other classes that extend Enumerable, and override #select and #reject (like Struct#select). If this patch is acceptable, I am happy to extend the patch for the other #select and #reject methods, and write tests. ---------------------------------------- Feature #9602: Logic with `Enumerable#grep` https://bugs.ruby-lang.org/issues/9602#change-45668 * Author: Tsuyoshi Sawada * Status: Feedback * Priority: Normal * Assignee: Yukihiro Matsumoto * Category: * Target version: ---------------------------------------- `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) -- http://bugs.ruby-lang.org/