可能重复:
什么!! 红宝石意味着什么?
嗨,
我是Ruby的新手,无法找到什么"!!"的描述 手段.
这是一个例子:
def signed_in?
!!current_user
end
Run Code Online (Sandbox Code Playgroud)
如果这是双重否定,为什么不说:
def signed_in?
current_user
end
Run Code Online (Sandbox Code Playgroud)
请帮忙.
pka*_*ing 136
在Ruby(以及许多其他语言)中,有许多值true在布尔上下文中进行求值,而少数值将求值为false.在Ruby中,评估的唯一两件事false是false(本身)和nil.
如果你否定了某些东西,就会强制一个布尔上下文.当然,它也否定了它.如果你对它进行双重否定,它会强制布尔上下文,但会返回正确的布尔值.
例如:
"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)
Run Code Online (Sandbox Code Playgroud)
在您的示例中,该signed_in?方法应返回一个布尔值(由?字符约定).它用来决定这个值的内部逻辑是检查current_user变量是否设置.如果已设置,它将true在布尔上下文中求值.如果没有,它将评估为假.双重否定强制返回值为布尔值.
The*_*heo 30
!!只是!(布尔否定运算符)写了两次.它将否定论证,然后否定否定.它很有用,因为您可以使用它从任何值获取布尔值.第一个!将参数转换为布尔值,例如,true如果它是,nil或者false,false否则.让你得到参数的布尔值,第二个将再次否定是false对nil还是false,true只是有关其他的一切.
在Ruby中,您可以在if语句中使用任何值,例如,if current_user如果当前用户不是,则执行nil.大多数情况下这很好,因为它节省了我们键入显式测试(比如if !current_user.nil?,至少六个字符更长).但是,如果在方法暗示它返回一个布尔值时返回一个对象,有时可能会让人感到困惑.名称?以此结尾的方法应返回真值或假值,即它们返回将评估为true或的值false.但是,如果signed_in?返回用户对象,它可能会变得非常混乱.例如,如果你试图调试为什么使用一些代码signed_in?不工作,你可能会得到真糊涂,当用户对象变成了您所预期true或false.在这种情况下是非常有用的添加!!之前return,因为这保证了truthy或falsy值将用作返回true或false.
| 归档时间: |
|
| 查看次数: |
40164 次 |
| 最近记录: |