!! - 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 .
Theo Oct 22 '10 at 5:11 2010-10-22 05:11
source share