Ruby,!! operator (a / k / a double hit)

Possible duplicate:
What is he doing!! in ruby?

Hello,

I am new to Ruby and cannot find anywhere else a description of what "!!" means.

Here is an example:

def signed_in? !!current_user end 

If it's double negative, why not say:

 def signed_in? current_user end 

Please, help.

+72
operators ruby
Oct 22 '10 at 5:05
source share
4 answers

In most programming languages, including Ruby,! will return the opposite to the logical value of the operand. Therefore, when you combine two exclamation points, they convert the value to a logical one.

+95
Oct 22 '10 at 5:07
source share

In Ruby (and many other languages), there are many values ​​that evaluate to true in a boolean context, and several that will evaluate to false. In Ruby, only two things that evaluate to false are false (itself) and nil .

If you deny something, it causes a Boolean context. Of course, it also denies this. If you double it, it forces a boolean context, but returns the correct boolean value.

For example:

 "hello" #-> this is a string; it is not in a boolean context !"hello" #-> this is a string that is forced into a boolean # context (true), and then negated (false) !!"hello" #-> this is a string that is forced into a boolean # context (true), and then negated (false), and then # negated again (true) !!nil #-> this is a false-y value that is forced into a boolean # context (false), and then negated (true), and then # negated again (false) 

In your example, the signed_in? method signed_in? should return a boolean value (as indicated by the symbol ? ). The internal logic used to determine this value is to check if the variable current_user . If set, it will evaluate to true in a boolean context. If not, it will be rated as false. Double negation causes the return value to be Boolean.

+121
Oct 22 '10 at 5:10
source share

!! - it's easy ! (boolean negation operator) written twice. This negates the argument, and then denies the negation. This is useful because you can use it to get a boolean from any value. First one ! converts an argument to a logical one, for example. true if it is nil or false , and false otherwise. The second one will lead to negation again, so that you get the boolean argument false for nil or false , true for everything else.

In Ruby, you can use any value in an if , for example. if current_user will be executed if the current user is not nil . Most of the time, this is great because it allows us to print explicit tests (for example, if !current_user.nil? Which are at least six characters long). But sometimes it can be really confusing if you return an object when the method implies that it returns a boolean value. Methods Whose Name Ends With ? must return true or false values, i.e. They return a value that will be evaluated as true or false . However, it can become really messy if signed_in? returned a user object. For example, if you are trying to debug, why some kind of code that uses signed_in? does not work, you are likely to really get confused when the user object ends up where you expected true or false . In this situation, it is useful to add !! before return , as this ensures that the value of truth or falsehood is returned as true or false .

+25
Oct 22 '10 at 5:11
source share

As you correctly understood, this is a bi-directional use of the operator ! . However, although this may be a shorthand way to check if a variable can be null or not, IMO is too brief. Take a look at this and this post. Note that in Ruby testing something for zero will evaluate to false.

+3
Oct 22 '10 at 5:13
source share



All Articles