def test
!!session[:test]
end
!! - what does this do? can we remove it and still assume it will work the same?
! - 这是做什么的?我们可以删除它并仍然认为它将工作相同吗?
3 个解决方案
#1
9
That would be the double bang (or bang bang).
这将是双重爆炸(或爆炸声)。
It's not really an operator in itself. It is really two !
operators together...which performs double negation. It is used to make sure you're working with a boolean value.
它本身并不是一个真正的运营商。真的是两个!运营商在一起...执行双重否定。它用于确保您使用布尔值。
#2
4
The first !
coerces its operand to a boolean value, then negates it. The second !
negates the negated value. The sum effect is to coerce the operand to a boolean type. So if you change it then the method won't be returning a boolean anymore, it will be returning whatever is in the hash for that symbol. Nothing will change if you remove it, but the advantage from using !!
is you can't abuse the method call to get the object from the session.
首先 !将其操作数强制转换为布尔值,然后取消它。第二 !否定否定的价值。和效应是将操作数强制转换为布尔类型。因此,如果您更改它,那么该方法将不再返回布尔值,它将返回该符号的哈希中的任何内容。如果你删除它没有什么会改变,但使用的优势!!是你不能滥用方法调用从会话中获取对象。
#3
0
To address your second question, your method will change subtly if you remove the double negation since you'll be returning the object instead of TrueClass or FalseClass.
为了解决你的第二个问题,如果你删除双重否定,你的方法将会巧妙地改变,因为你将返回对象而不是TrueClass或FalseClass。
The !! is generally frowned upon unless you explicitly need boolean values (for example, if you're building out an API). Since Ruby evaluates any non-nil and non-false value as truthy, it's usually safe just to return the object in question so that you can call its methods.
!!除非您明确需要布尔值(例如,如果您正在构建API),否则通常不赞成。由于Ruby将任何非零和非假值评估为真实,因此返回有问题的对象通常是安全的,以便您可以调用其方法。
#1
9
That would be the double bang (or bang bang).
这将是双重爆炸(或爆炸声)。
It's not really an operator in itself. It is really two !
operators together...which performs double negation. It is used to make sure you're working with a boolean value.
它本身并不是一个真正的运营商。真的是两个!运营商在一起...执行双重否定。它用于确保您使用布尔值。
#2
4
The first !
coerces its operand to a boolean value, then negates it. The second !
negates the negated value. The sum effect is to coerce the operand to a boolean type. So if you change it then the method won't be returning a boolean anymore, it will be returning whatever is in the hash for that symbol. Nothing will change if you remove it, but the advantage from using !!
is you can't abuse the method call to get the object from the session.
首先 !将其操作数强制转换为布尔值,然后取消它。第二 !否定否定的价值。和效应是将操作数强制转换为布尔类型。因此,如果您更改它,那么该方法将不再返回布尔值,它将返回该符号的哈希中的任何内容。如果你删除它没有什么会改变,但使用的优势!!是你不能滥用方法调用从会话中获取对象。
#3
0
To address your second question, your method will change subtly if you remove the double negation since you'll be returning the object instead of TrueClass or FalseClass.
为了解决你的第二个问题,如果你删除双重否定,你的方法将会巧妙地改变,因为你将返回对象而不是TrueClass或FalseClass。
The !! is generally frowned upon unless you explicitly need boolean values (for example, if you're building out an API). Since Ruby evaluates any non-nil and non-false value as truthy, it's usually safe just to return the object in question so that you can call its methods.
!!除非您明确需要布尔值(例如,如果您正在构建API),否则通常不赞成。由于Ruby将任何非零和非假值评估为真实,因此返回有问题的对象通常是安全的,以便您可以调用其方法。