Skip to content

End of Life Ruby 3.0 has reached end of life and gets no more fixes. Switch to Ruby 4.0 →

instance method

Enumerable#grep_v

enum.grep_v(pattern) → array
enum.grep_v(pattern) { |obj| block } → array

Inverted version of Enumerable#grep. Returns an array of every element in enum for which not Pattern === element.

Example
(1..10).grep_v 2..5   #=> [1, 6, 7, 8, 9, 10]
res =(1..10).grep_v(2..5) { |v| v * 2 }
res                    #=> [2, 12, 14, 16, 18, 20]
See also #grep #group_by #flat_map #include? also in 4.1 · 4.0 · 3.4 · 3.3 · 3.2 · 3.1