On Jul 29, 2011, at 6:04 AM, Rick DeNatale wrote:

> On Thu, Jul 28, 2011 at 1:04 PM, David Chelimsky <dchelim...@gmail.com> wrote:
> On Jul 28, 2011, at 11:40 AM, Roger Pack wrote:
> 
> >>> 'abc' ~! /def/
> > => true
> >
> >     'abc'.should !~ /def/
> >
> > fails though.  Seemed unexpected...
> > -roger-
> 
> This comes up from time to time but it's a bitch to google for. It boils down 
> to this: the only way to support "actual.should != unexpected" in Ruby 1.8 is 
> to go back and parse the file. This is because == is a method but != is not a 
> method: it's handled by the parser. What that means is this:
> 
> 5.should == 5
> # becomes
> 5.should.==(5)
> 
> 5.should != 4
> # becomes
> !(5.should.==(4))
> 
> In the latter case the code evaluating 5 == 4 has no way to know that it's 
> been negated.
> 
> HTH,
> David
> 
> All true David, but you might want to get your eyeglass prescription checked, 
> or maybe you glossed over the difference between = and ~.

Probably a bit of both :)

> Roger is using != but !~  which is the negated form of ~=,  although I think 
> !~ uses the same kind of compile time expansion as !=.

That appears to be the case.

> And then, Roger is trying to transpose the characters to ~!  which is 
> syntactically incorrect sugar. Although this might be a typo in the post.
> 
> → irb
> >> 'abc' !~ /def/
> => true
> >> 'abc' ~! /def/
> SyntaxError: compile error
> (irb):2: syntax error, unexpected '~', expecting $end
> 'abc' ~! /def/
>        ^
>       from (irb):2
> 
> 
> 
> And of course the solution is to use should_not
> 
>     'abc'.should_not =~ /def/
> or
>    'abc'.should_not match(/def/)

I prefer the latter, which leads me to prefer 'abc'.should match(/bc/) as well.

Thanks for setting things straight.

Cheers,
David

> -- 
> Rick DeNatale
> 
> Blog: http://talklikeaduck.denhaven2.com/
> Github: http://github.com/rubyredrick
> Twitter: @RickDeNatale
> WWR: http://www.workingwithrails.com/person/9021-rick-denatale
> LinkedIn: http://www.linkedin.com/in/rickdenatale

_______________________________________________
rspec-users mailing list
rspec-users@rubyforge.org
http://rubyforge.org/mailman/listinfo/rspec-users

Reply via email to