Ruby equivalent to grep -v Ruby equivalent to grep -v ruby ruby

Ruby equivalent to grep -v


Ruby 2.3 implements an Enumerable#grep_v method that is exactly what you're after.

https://ruby-doc.org/core-2.3.0/Enumerable.html#method-i-grep_v


You know how Symbol#to_proc helps with chaining? You can do the same with regular expressions:

class Regexp  def to_proc    Proc.new {|string| string =~ self}  endend["Ruby", "perl", "Perl", "PERL"].reject(&/perl/i)=> ["Ruby"]

But you probably shouldn't. Grep doesn't just work with regular expressions - you can use it like the following

[1,2, "three", 4].grep(Fixnum)

and if you wanted to grep -v that, you'd have to implement Class#to_proc, which sounds wrong.


How about this?

arr = ["abc", "def", "aaa", "def"]arr - arr.grep(/a/)  #=> ["def", "def"]

I deliberately included a dup to make sure all of them are returned.