红宝石&&整数运算符 [英] Ruby && operator on integers

查看:79
本文介绍了红宝石&&整数运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人知道&&的含义吗?和||应用于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

推荐答案

布尔运算符&&||andor是少数几个不是方法调用语法的糖,因此,在不同的类中没有不同的实现.

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 Integers as they do for Strings, Hashes, Floats, Symbols, 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)

nilfalse是假的,其他所有东西都是真实的,包括00.0''[]{}.

nil and false are falsey, everything else is truthy, including 0, 0.0, '', [], and {}.

这篇关于红宝石&&整数运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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