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
  • 259,732
  • 324
  • 769
  • 1,199

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
  • 510,633
  • 85
  • 743
  • 889
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
  • 35,455
  • 10
  • 90
  • 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
  • 223,387
  • 19
  • 210
  • 288