0

Possible Duplicate:
What does !! mean in ruby?

I found !! in Paypal gem here: https://github.com/tc/paypal_adaptive/blob/master/lib/paypal_adaptive/config.rb like 59

but I don't understand what it does.

I know that ! means NOT, but !! doesn't make sense.

here's the screen: http://tinyurl.com/7acklhr

Community
  • 1
  • 1
Sasha
  • 20,424
  • 9
  • 40
  • 57

4 Answers4

4

It forces any value to true or false depending on its "truthy" nature.

This is simply because, as you've noted, ! is the Boolean-not operator. For instance:

t = 1
puts !t  # => false
puts !!t # => true
f = nil
puts !f  # => true
puts !!f # => false
Chowlett
  • 45,935
  • 20
  • 116
  • 150
4

The !! is used to return either true or false on something that returns anything :

In Ruby, everything other than nil and false is interpreted as true. But it will not return true, it will return the value. So if you use !, you get true or false but the opposite value of what is really is. If you use !!, you get the true or false corresponding value.

Ingolmo
  • 575
  • 3
  • 12
1

It is used to make sure its the boolean type.

Explanation more detailed

Eg:

!!active => true

active = false => false

!!active => false

active = nil => nil

!!active => false

Pedro Ferreira
  • 629
  • 3
  • 8
0

This forces a result to be true or false. As in ruby nil is not exactly false this can be useful. For instance:

def try x
  if x == 1
    return nil
  else
    return "non-nil"
  end
end

p "try1" if try(1) # here you get a string printed
p "try2" if !!try(1) # here you don't
Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176