为何真理&& “字符串"返回“字符串" [英] Why does truth && "string" return "string"

查看:140
本文介绍了为何真理&& “字符串"返回“字符串"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有类似的东西

true && true #=> true

这很有道理,所以我尝试类似的事情:

Which makes sense, so I try something like:

true && "dsfdsf" #=> "dsfdsf"

这让我感到惊讶,因为很多时候我会做类似if something && something的事情,而我一直认为这样做会评估为true并会返回true.进行进一步的实验,例如:

Which surprises me because often times I'll do something like if something && something and I always thought that that was evaluating to true and would return true. Further experimentation doing things like:

jruby-1.7.3 :009 > "ad" && "dsf"
 => "dsf" 
jruby-1.7.3 :010 > "ad" && "sdfd" && nil
 => nil 
jruby-1.7.3 :011 > "ad" && nil && "sdf"
 => nil 

使得Ruby似乎返回了所有值均为true的最后一个值或找到的第一个false值.为什么这样做呢?这是正确的心理模型吗?

makes it seem like Ruby returns either the last value if all are true or the first false value it finds. Why does it do this? and is this a correct mental model?

推荐答案

ruby​​中的布尔运算符是短路:如果可以根据左侧参数确定表达式的值,则不评估右侧参数.

Boolean operators in ruby are short-circuiting: if it is possible to determine the value of the expression from the left-hand argument, the right-hand argument isn't evaluated.

因此,一个用于评估包含&&的布尔表达式的更简单的思维模型是考虑仅涉及两个操作数的第一个表达式:左边的操作数首先被计算;第二个操作数被首先计算.如果此操作数的值为nilfalse,则返回该操作数,并且不评估右侧操作数;如果左侧的操作数是其他任何值,则将评估右侧的运算符并返回其值.

Therefore, a simpler mental model for evaluation of a boolean expression involving && is to consider first expressions involving only two operands: the left-hand operand is evaluated first; if the value of this operand is nil or false, the operand is returned and the right-hand operand isn't evaluated; if the left-hand operand is anything else, the right-hand operator is evaluated and its value is returned.

根据此定义,很明显,正如您所注意到的,涉及布尔运算符的表达式不会返回truefalse,而只是返回 true值 false值.值得注意的是,在布尔表达式仅用于其 true-ness false-ness 的上下文中,这没有任何区别.

From this definition, it's clear that, as you note, expressions involving boolean operators don't return true or false, but simply a true value or a false value. It's worth noting that this doesn't make any difference in a context where a boolean expression is used only for its true-ness or false-ness.

由于布尔运算符是左关联的,因此很容易确定包含多个运算符的表达式的求值顺序,请记住&&的优先级高于||(但是请注意,andor具有相同的优先级).完成此操作后,我们可以很容易地看到表达式的值是最后求值的元素,即可以确定整体 true-ness false-ness 的元素的表达式.

Being the boolean operators left-associative, it's easy to determine the order of evaluation of an expression containing more than one operator, remembering that && has higher precedence than || (be careful however that and and or have the same precedence). Having done this, we can easily see that the value of the expression is the last evaluated element, i.e. the element that permits to determine the overall true-ness or false-ness of the expression.

在您的示例(仅由&&运算符组成的表达式)中,一旦遇到第一个 false值,或者在最后一个元素被评估为后,该表达式的值就会被知道真实值;因此,如果在其之前的所有元素都为 true值,则最后评估的元素将为最后一个元素;如果遇到任何第一个 false值,则为最后一个元素.

In your examples (an expression composed only by && operators) the value of the expression is known as soon as the first false value is encountered, or after the last element has been evaluated as a true value; so, the last element evaluated will be the last element if all the elements preceding it are true valued, and the first false valued element if any is encountered.

您可能想知道为什么表达式的值没有转换为truefalse;实际上,这种行为可以用在像这样的习语中

You might wonder why the value of the expression isn't converted to true or false; actually, this kind of behavior can be used in idioms like

x = x || default

或更简短的

x ||= default

用于检查x是否为nil

,在这种情况下,为其分配默认值.

that is used to check if x is nil and, in that case, assign a default value to it.

这篇关于为何真理&& “字符串"返回“字符串"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆