What does !! mean in ruby?

Just wondering what !! is in Ruby.


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.


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