--00235446fcd425a54704b67ddb0c Content-Type: text/plain; charset=UTF-8 On Sat, Jan 14, 2012 at 2:24 PM, Robert Klemme <shortcutter / googlemail.com>wrote: > On Sat, Jan 14, 2012 at 1:51 PM, Josh Cheek <josh.cheek / gmail.com> wrote: > > On Sat, Jan 14, 2012 at 6:06 AM, Robert Klemme > > <shortcutter / googlemail.com>wrote: > > > >> > >> It's impossible because > >> > >> - you cannot change behavior of ! > >> > > > > You can do this (I'm not advocating it), but IDK how you would actually > > negate it. > > > > class Regexp > > def !@ > > /some sort of negation of #{source}/ > > end > > end > > > > abc abc/ > > !abc # /some sort of negation of abc/ > > Oh, I wasn't aware of this. Thank you for the education! > > The way to use it would probably be to create a wrapper instance which > reverses semantics of the original's Regexp instance and sets global > variables accordingly (if this is possible; not sure in the case of > $1, $2 etc. because of their special nature). > > Still the other obstacle would prevent making !enum.grep(/foo/) return > what is intended. You could only do enum.grep(!/foo/). There is a > potential though to break other code which relies on different boolean > behavior of Regexp instances so the approach with #grep_v or an > explicit Regexp#negate would be better. > I meant my earlier proposal > Negation could be an option for the Enumerable grep command more along these lines: describe "grep_with_options" do it "works as before with 1 arugment" do %w{abc def ghi}.grep(/abc/).should %w{abc} end it "negates the selection with 'v'" do %w{abc def ghi}.grep(/abc/, 'v').should %w{def ghi} end it "can be chained" do %w{abc adf agh bcd efd}.grep(/^a/).grep(/c/,'v').should %w{adf agh} end it "can select lines after with A" do pending("TODO") %w{abc def ghi}.grep(/abc/, 'A1').should %w{abc def} end end # a naive implementation module Enumerable def grep(cond, *select_options) negate_matches elect_options.include?('v') if negate_matches reject {|e| cond e} else select {|e| cond e} end end end Just the 'v' option to grep would already be useful (and it would match with the request from the OP). There are other grep options that I use quite often too and could be implemented (-A -B -C -c ...) similarly. Peter --00235446fcd425a54704b67ddb0c--