!~ seems to not work

‘abc’ ~! /def/
=> true

 'abc'.should !~ /def/

fails though. Seemed unexpected…
-roger-

On Jul 28, 2011, at 11:40 AM, Roger P. 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

On Thu, Jul 28, 2011 at 1:04 PM, David C.
[email protected]wrote:

This comes up from time to time but it’s a bitch to google for. It boils
!(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 ~.

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

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/)


Rick DeNatale

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

On Fri, Jul 29, 2011 at 7:59 AM, David C.
[email protected]wrote:

Thanks for setting things straight.

Just happy to be here sir!


Rick DeNatale

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

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

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 :slight_smile:

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.

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