Cameron
Cameron

Reputation: 4201

What does !! mean in ruby?

Just wondering what !! is in Ruby.

Upvotes: 140

Views: 19769

Answers (8)

Alex Wayne
Alex Wayne

Reputation: 187014

Not not.

It's used to convert a value to a boolean:

!!nil   #=> false
!!"abc" #=> true
!!false #=> false

It's usually not necessary to use though since the only false values to Ruby are nil and false, so it's usually best to let that convention stand.

Think of it as

!(!some_val)

One thing that is it used for legitimately is preventing a huge chunk of data from being returned. For example you probably don't want to return 3MB of image data in your has_image? method, or you may not want to return your entire user object in the logged_in? method. Using !! converts these objects to a simple true/false.

Upvotes: 170

Richard-Degenne
Richard-Degenne

Reputation: 2949

Other answers have discussed what !! does and whether it is good practice or not.

However, none of the answers give the "standard Ruby way" of casting a value into a boolean.

true & variable

TrueClass, the class of the Ruby value true, implements a method &, which is documented as follows:

Returns false if obj is nil or false, true otherwise.

Why use a dirty double-negation when the standard library has you covered?

Upvotes: -1

Micah Elliott
Micah Elliott

Reputation: 10264

It is "double-negative", but the practice is being discouraged. If you're using rubocop, you'll see it complain on such code with a Style/DoubleNegation violation.

The rationale states:

As this is both cryptic and usually redundant, it should be avoided [then paraphrasing:] Change !!something to !something.nil?

Upvotes: 3

inopinatus
inopinatus

Reputation: 3779

Understanding how it works can be useful if you need to convert, say, an enumeration into a boolean. I have code that does exactly that, using the classy_enum gem:

class LinkStatus < ClassyEnum::Base
  def !
    return true
  end
end

class LinkStatus::No < LinkStatus
end

class LinkStatus::Claimed < LinkStatus
  def !
    return false
  end
end

class LinkStatus::Confirmed < LinkStatus
  def !
    return false
  end
end

class LinkStatus::Denied < LinkStatus
end

Then in service code I have, for example:

raise Application::Error unless !!object.link_status   # => raises exception for "No" and "Denied" states.

Effectively the bangbang operator has become what I might otherwise have written as a method called to_bool.

Upvotes: 0

August Lilleaas
August Lilleaas

Reputation: 54593

! means negate boolean state, two !s is nothing special, other than a double negation.

!true == false
# => true

It is commonly used to force a method to return a boolean. It will detect any kind of truthiness, such as string, integers and what not, and turn it into a boolean.

!"wtf"
# => false

!!"wtf"
# => true

A more real use case:

def title
  "I return a string."
end

def title_exists?
  !!title
end

This is useful when you want to make sure that a boolean is returned. IMHO it's kind of pointless, though, seeing that both if 'some string' and if true is the exact same flow, but some people find it useful to explicitly return a boolean.

Upvotes: 14

RichH
RichH

Reputation: 6138

It returns true if the object on the right is not nil and not false, false if it is nil or false

def logged_in?   
  !!@current_user
end

Upvotes: 31

Andrew Grimm
Andrew Grimm

Reputation: 81500

It's useful if you need to do an exclusive or. Copying from Matt Van Horn's answer with slight modifications:

1 ^ true
TypeError: can't convert true into Integer

!!1 ^ !!true
=> false

I used it to ensure two variables were either both nil, or both not nil.

raise "Inconsistency" if !!a ^ !!b

Upvotes: 3

Tom
Tom

Reputation: 10819

Note that this idiom exists in other programming languages as well. C didn't have an intrinsic bool type, so all booleans were typed as int instead, with canonical values of 0 or 1. Takes this example (parentheses added for clarity):

!(1234) == 0
!(0) == 1
!(!(1234)) == 1

The "not-not" syntax converts any non-zero integer to 1, the canonical boolean true value.

In general, though, I find it much better to put in a reasonable comparison than to use this uncommon idiom:

int x = 1234;
if (!!x); // wtf mate
if (x != 0); // obvious

Upvotes: 6

Related Questions