红宝石,!!运算符(又称双声)
可能的重复:
什么是!!在红宝石中是什么意思?
嗨,
我是 Ruby 新手,找不到任何关于“!!”的描述。方法。
这是一个例子:
def signed_in?
!!current_user
end
如果这是双重否定,为什么不说:
def signed_in?
current_user
end
请帮忙。
Possible Duplicate:
What does !! mean in ruby?
Hi,
I'm new to Ruby and can't find anywhere description of what "!!" means.
Here's an example:
def signed_in?
!!current_user
end
If this is a double negative, why not to say:
def signed_in?
current_user
end
Please help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 Ruby(以及许多其他语言)中,有许多值在布尔上下文中计算结果为 true,也有少数值计算结果为 false。在 Ruby 中,计算结果为
false
的唯一两件事是false
(本身)和nil
。如果你否定某些东西,就会强制使用布尔上下文。当然,它也否定它。如果对它进行双重否定,它会强制布尔上下文,但返回正确的布尔值。
例如:
在您的示例中,
signed_in?
方法应返回一个布尔值(按照约定由?
字符指示)。它用来决定该值的内部逻辑是通过检查current_user
变量是否已设置。如果设置了,它将在布尔上下文中计算为true
。如果不是,它将评估为 false。双重否定强制返回值为布尔值。In Ruby (and many other languages) there are many values that evaluate to
true
in a boolean context, and a handful that will evaluate to false. In Ruby, the only two things that evaluate tofalse
arefalse
(itself) andnil
.If you negate something, that forces a boolean context. Of course, it also negates it. If you double-negate it, it forces the boolean context, but returns the proper boolean value.
For example:
In your example, the
signed_in?
method should return a boolean value (as indicated by convention by the?
character). The internal logic it uses to decide this value is by checking to see if thecurrent_user
variable is set. If it is set, it will evaluate totrue
in a boolean context. If not, it will evaluate as false. The double negation forces the return value to be a boolean.在大多数编程语言(包括 Ruby)中,
!
将返回操作数的布尔值的相反值。因此,当您将两个感叹号链接在一起时,它会将值转换为布尔值。In most programming languages, including Ruby,
!
will return the opposite of the boolean value of the operand. So when you chain two exclamation marks together, it converts the value to a boolean.!!
只是写了两次!
(布尔否定运算符)。它将否定论证,然后否定否定。它很有用,因为您可以使用它从任何值获取布尔值。第一个!
会将参数转换为布尔值,例如,如果为nil
或false
,则为true
,并且false
否则。第二个将再次否定它,以便您获得参数的布尔值,false
表示nil
或false
,true 几乎所有其他内容。
在 Ruby 中,您可以在
if
语句中使用任何值,例如,如果当前用户不是nil
,则if current_user
将执行。大多数时候,这很棒,因为它可以节省我们输入显式测试的时间(例如if !current_user.nil?
,它至少长六个字符)。但有时,如果您返回一个对象,而该方法暗示它返回一个布尔值,那么有时可能会非常令人困惑。名称以?
结尾的方法应该返回 true 或 false 值,即它们返回计算结果为true
或false
的值。但是,如果signed_in?
返回用户对象,它可能会变得非常混乱。例如,如果您尝试调试为什么某些使用signed_in?
的代码不起作用,当用户对象出现在您期望的true
位置时,您可能会感到非常困惑或假
。在这种情况下,在return
之前添加!!
很有用,因为这样可以保证 true 或 false 值将以true
或 <代码>假。!!
is just!
(the boolean negation operator) written twice. It will negate the argument, then negate the negation. It's useful because you can use it to get a boolean from any value. The first!
will convert the argument to a boolean, e.g.true
if it'snil
orfalse
, andfalse
otherwise. The second will negate that again so that you get the boolean value of the argument,false
fornil
orfalse
,true
for just about everything else.In Ruby you can use any value in an
if
statement, e.g.if current_user
will execute if the current user is notnil
. Most of the time this is great because it saves us typing explicit tests (likeif !current_user.nil?
, which is at least six characters longer). But sometimes it might be really confusing if you return an object when the method implies that it returns a boolean. Methods whose name ends with?
should return truthy or falsy values, i.e. they return something that will evaluate totrue
orfalse
. However, it can get really messy ifsigned_in?
returned a user object. For example if you're trying to debug why some code that usessigned_in?
doesn't work you will probably get really confused when a user object turns up where you expectedtrue
orfalse
. In that situation it's useful to add!!
before thereturn
since that guaranteed that the truthy or falsy value will be returned as eithertrue
orfalse
.正如您正确理解的那样,它是
!
运算符的双重否定用法。也就是说,虽然它可以是检查变量是否可以为 nil 的简写方法,但在我看来,这太简洁了。看看这个 和这篇文章。请注意,在 Ruby 中,测试某些内容为 nil 将计算为 false。As you rightly understood it is a double-negative use of the
!
operator. That said, while it can be a shorthand way to check for whether a variable can be nil or not, IMO that's too concise. Take a look at this and this post. Note that in Ruby, testing something to nil will evaluate to false.