-1

Actually I am new to rails I was trying to build authentication from scratch but didn't have any idea. So I was reading some code on internet and I am struck at a function.What does !! means over here. Is it first making it false then true? What it is and why we are using it?

def logged_in?
  !!current_user
end
RAJ
  • 9,697
  • 1
  • 33
  • 63
prateek
  • 1
  • 5
  • Possible duplicate of [What does !! mean in ruby?](http://stackoverflow.com/questions/524658/what-does-mean-in-ruby) – Fabian Winkler Dec 15 '15 at 10:41
  • the link was not helpful – prateek Dec 15 '15 at 10:43
  • You can either use `current_user` or `!current_user` – Surge Dec 15 '15 at 10:45
  • `current_user` returns either an user object or `nil`, but we want `true` or `false`. `!!` does it. – Mareq Dec 15 '15 at 10:46
  • The second most upvoted answer (http://stackoverflow.com/a/524688/1235795) even uses the same code snippet as you do. So I really don't get why it doesn't answer your question. – Fabian Winkler Dec 15 '15 at 10:46
  • It is. `!!current_user` will look up if the var `current_user` is an filled object / string / whatever. If yes it's `true`, if not it returns `false`. So the value for `logged_in` is `false` if `current_user` is not a string or something similar. – Hecke29 Dec 15 '15 at 10:47

3 Answers3

0

We use double bang to get value as Boolean (true/false). Here is demonstration:

2.1.2 :001 > my_var = "string"
 => "string" 
2.1.2 :002 > !my_var
 => false 
2.1.2 :003 > !!my_var
 => true

2.1.2 :005 > null_var = nil
 => nil 
2.1.2 :006 > !!null_var
 => false 

So, as you can see that, if we want to get corresponding boolean value for an object then we use double bang (!!).

In your case, I assume current_user can have user object OR nil value. Instead of getting user object or nil, it's using !! to convert it into corresponding boolean.

Hope it helps. Happy Learning!

EDIT: I would like to add that in Ruby, it's convention that method name ending with ? will return boolean value. So, your mentioned code us using !! in logged_in? to return boolean instead of User or NULL class objects.

RAJ
  • 9,697
  • 1
  • 33
  • 63
0

! means "not", so !! means "not-not". ! is often called "bang" (because 'exclamation mark' is too long, i assume) and so !! is often called "double bang". See also "hashbang" or "shebang" which mean #!.

It has the effect of converting all "truthy"* things into the boolean true and all "falsy" things into the boolean false.

Note that in Ruby, unlike some languages, the only things that are "falsy" are false and nil. 0, "", [], {}, and any other object that isn't false or nil, such as a User object, are "truthy".

*"truthy" means it will pass an "if" test.

Max Williams
  • 32,435
  • 31
  • 130
  • 197
0

You are trying to do something like this

def logged_in?
  current_user

you are asking here if it is a current user

def logged_in?
  !current_user

while you are asking here is the logged in user is not current_user.

Surge
  • 256
  • 3
  • 16