红宝石&&整数运算符 [英] Ruby && operator on integers
问题描述
有人知道&&的含义吗?和||应用于Ruby中的整数时?
does anyone know the meaning of && and || when applied to integers in Ruby?
以下是IRB中&&和||应用于整数:
Here are some examples in IRB when && and || are applied to integers:
>> 1 && 2
=> 2
>> 2 && 1
=> 1
>> 15 && 20
=> 20
>> 20 && 15
=> 15
>> 0 && -1
=> -1
>> -1 && -2
=> -2
>> 1 || 2
=> 1
>> 2 || 1
=> 2
推荐答案
布尔运算符&&
,||
,and
和or
是少数几个不是方法调用语法的糖,因此,在不同的类中没有不同的实现.
The boolean operators &&
, ||
, and
and or
are among the few that are not syntactic sugar for method calls, and thus, there are no different implementations in different classes.
换句话说:它们对所有对象的行为完全相同,即,它们对Integer
的行为与对String
的行为相同,Hash
es,Float
s,Symbol
s和任何其他对象.
In other words: they behave exactly the same for all objects, i.e. they behave the same for Integer
s as they do for String
s, Hash
es, Float
s, Symbol
s, and any other object.
a && b
如果a
是假的,则
评估为a
(在这种情况下将完全不评估b
),否则将评估为b
(只有这样,才会评估b
)
evaluates to a
if a
is falsey (and will not evaluate b
at all in this case), otherwise it evaluates to b
(and only then will b
be evaluated)
a || b
如果a
是真实的,则
评估为a
(在这种情况下完全不会评估b
),否则它将评估为b
(只有这样,才会评估b
)
evaluates to a
if a
is truthy (and will not evaluate b
at all in this case), otherwise it evaluates to b
(and only then will b
be evaluated)
nil
和false
是假的,其他所有东西都是真实的,包括0
,0.0
,''
,[]
和{}
.
nil
and false
are falsey, everything else is truthy, including 0
, 0.0
, ''
, []
, and {}
.
这篇关于红宝石&&整数运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!