airbnb / ruby

Ruby Style Guide

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The negation operator

RomanMenzel opened this issue · comments

When I try to use the negation operator without parens, it doesn't work!
With which ruby version did you try the examples?

Can you provide a code example? x = 3; -x prints out negative three in every ruby console I've ever used.

No, I mean the '!' operator. An example would be:

if !x == 3
    ...
end

This doesn't work! ☝️

You want if x != 3 there. negation applies to a value, and !x will never == 3, because !x will always be a boolean true or false.

Ok, now I understand, thank you.