Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what does !! do in ruby?

I am using some code that uses this syntax (restful authentication).

def logged_in?
  !!current_user()
end

Tried googling for this but it just seems to ignore "!!", will accept an answer that can tell me how to find info about searching for strings such as !! in google.

like image 469
Jim Avatar asked Jan 13 '10 17:01

Jim


People also ask

What does '@' mean in Ruby?

Instance variables Examples: @foobar. The variable which name begins which the character ` @ ', is an instance variable of self . Instance variables are belong to the certain object. Non-initialized instance variables has value nil .

What does &: mean in Ruby?

What you are seeing is the & operator applied to a :symbol . In a method argument list, the & operator takes its operand, converts it to a Proc object if it isn't already (by calling to_proc on it) and passes it to the method as if a block had been used.

What does exclamation mark do in Ruby?

It is just a naming convention To Ruby the exclamation mark, or bang (!), does not mean anything internally, it is just a naming convention that Ruby programmers use to denote that a method can be “dangerous”. According to this convention, the exclamation mark(!)

What is symbol in Ruby?

What's a Symbol in Ruby? A symbol is a unique instance of the Symbol class which is generally used for identifying a specific resource. A resource can be: a method.


1 Answers

It's double negation. The first ! converts it to false if current_user is not nil or false. After that it converts it to true. So the result is always a boolean value and not the value of current_user. The result is always true if current_user is not false or nil. Otherwise it's false.

like image 104
Tomas Markauskas Avatar answered Oct 10 '22 15:10

Tomas Markauskas