1

Possible Duplicate:
What does !! mean in ruby?

what is this function doing?

def current_product?
   !!current_product
end

Isn't that a double negative?

Community
  • 1
  • 1
Blankman
  • 248,432
  • 309
  • 736
  • 1,161

3 Answers3

5

!! is basically a cast to boolean. If current_product is truthy, !current_product is false and !!current_product is true, and vice versa. I.e. it converts truthy values to true and falsy values to false.

deceze
  • 491,798
  • 79
  • 706
  • 853
1

It's effectively a cast/conversion to boolean.

Similar question, but for C++: Doube Negation in C++ code

Also a pretty decent post about it here: !! (The double bang / double not) in Ruby

Community
  • 1
  • 1
eldarerathis
  • 34,279
  • 10
  • 88
  • 93
1

This is a pattern you'll see in any language where every object has a truth value, but there are canonical booleans (whether they be called True and False, 1 and 0, 1 and "", t and nil, whatever). !!x is essentially a "cast to boolean", in that !!x has the same truth-value as x, but !!x will always be one of the canonical true/false values, instead of any old true/false value.

hobbs
  • 206,796
  • 16
  • 199
  • 282