红宝石,!!运算符(又称双声)

发布于 2024-09-28 06:19:19 字数 389 浏览 4 评论 0原文

可能的重复:
什么是!!在红宝石中是什么意思?

嗨,

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

给我一枪 2024-10-05 06:19:19

在 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)

在您的示例中,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 to false are false (itself) and nil.

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:

"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 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 the current_user variable is set. If it is set, it will evaluate to true in a boolean context. If not, it will evaluate as false. The double negation forces the return value to be a boolean.

一直在等你来 2024-10-05 06:19:19

在大多数编程语言(包括 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.

勿忘初心 2024-10-05 06:19:19

!! 只是写了两次 ! (布尔否定运算符)。它将否定论证,然后否定否定。它很有用,因为您可以使用它从任何值获取布尔值。第一个 ! 会将参数转换为布尔值,例如,如果为 nilfalse,则为 true,并且 false 否则。第二个将再次否定它,以便您获得参数的布尔值,false 表示 nilfalsetrue 几乎所有其他内容。

在 Ruby 中,您可以在 if 语句中使用任何值,例如,如果当前用户不是 nil,则 if current_user 将执行。大多数时候,这很棒,因为它可以节省我们输入显式测试的时间(例如 if !current_user.nil?,它至少长六个字符)。但有时,如果您返回一个对象,而该方法暗示它返回一个布尔值,那么有时可能会非常令人困惑。名称以 ? 结尾的方法应该返回 true 或 false 值,即它们返回计算结果为 truefalse 的值。但是,如果 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's nil or false, and false otherwise. The second will negate that again so that you get the boolean value of the argument, false for nil or false, 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 not nil. Most of the time this is great because it saves us typing explicit tests (like if !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 to true or false. However, it can get really messy if signed_in? returned a user object. For example if you're trying to debug why some code that uses signed_in? doesn't work you will probably get really confused when a user object turns up where you expected true or false. In that situation it's useful to add !! before the return since that guaranteed that the truthy or falsy value will be returned as either true or false.

晒暮凉 2024-10-05 06:19:19

正如您正确理解的那样,它是 ! 运算符的双重否定用法。也就是说,虽然它可以是检查变量是否可以为 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文