ruby三⽬运算符_Ruby中的运算符和表达式
ruby 三⽬运算符
In this lesson, we are going to look at the different operators that ruby contains and how to u them in the expressions.
在本课程中,我们将研究ruby包含的不同operators以及如何在expressions使⽤它们。
Ruby:加法运算符 (Ruby: Addition Operator)
+ symbol is ud. It is both binary and unary operator. As the name suggests a Binary operator needs two operands/values on the either side of the operator to perform an operation and a unary operator needs just a single operand. Unary Plus rves no purpo, it is prent just for the symmetry with unary minus.
+符号。 它既是⼆进制运算符⼜是⼀元运算符。 顾名思义, Binary operator在运算符的任⼀侧都需要两个操作数/值来执⾏运算,⽽⼀元运算符只需要⼀个运算数。 ⼀元加号(Unary Plus)毫⽆⽤处,仅出于⼀元减号的对称性⽽存在。
For example : 1 + 2 //binary addition
value = +3 //unary plus
Ruby:减法运算符 (Ruby: Subtraction Operator)
- symbol is ud. You can u unary minus to rever sign of a variable.
-符号。 您可以使⽤⼀元减号来反转变量的符号。
For Example : 23.2 - 2.2 //binary subtraction
tiktok怎么读value = -3 //unary minus
Ruby:乘法运算符 (Ruby: Multiplication Operator)
* symbol is ud. Perfoms Multiplication on two numeric operands.
*符号。 在两个数字操作数上执⾏Perfoms乘法。
For Example : 122 * 3.14
Ruby:除法运算符 (Ruby: Division Operator)
/
symbol is ud. Returns result of devision of the first numeric operand by cond operand.
/符号被使⽤。 返回第⼆个操作数除以第⼀个数字操作数的结果。
For Example : 18 / 3
Ruby:模运算符 (Ruby: Modulo Operator)
take it to the head
Returns remainder after division.
返回除法后的余数。
For Example : 13 % 2 //returns 1
Ruby:指数运算符 (Ruby: Exponential Operator)
If you want to rai x to the power of y (i.e) x ^ y.
如果要将x提⾼到y的幂(即x ^ y 。
It is done by x ** y
2 raid to the power 4 returns 16.
2提升到4的幂返回16。
场景英文
Ruby:运算符优先级 (Ruby: Operator Precedence)
Operators have some order of precedence which determines the order in which an expression will be evaluated.
Operators具有⼀些precedence顺序,该precedence确定了表达式的求值顺序。
Consider this example, 1 + 2 * 3
multiplication,
1 +
2 is performed and the result 3
3 will be multiplied by 33 and gives 99. But the multiplication
We might think that the 1 + 2
subtraction operators. Therefore, 2 *3
2 *
3 is
addition and subtraction
division
division and exponential
exponential operator have higher precedence than addition
performed first and the result is added to 11 and gives 77 as an answer.
加法和减法
减法运算符。 因此,⾸先执我们可能会认为执⾏了1 + 2
指数运算符的优先级⾼于加法
除法和指数
1 + 2并将结果33乘以33得到99 。 但是乘法
乘法 , 除法
2 * 3并将结果加到11并给出77作为答案。
⾏2 * 3
1 +
2 * 3, if However, we can modify the order of precedence by putting a subexpression in parenthes. In the expression 1 + 2 * 3
1 +
2 need to be performed first, put that expression in parenthes.
1 +
2 ,则将该表达式放在但是,我们可以通过在括号中放置⼀个⼦表达式来修改优先顺序。 在表达式1 + 2 * 3中
1 +
2 * 3中 ,如果⾸先需要执⾏1 + 2
括号中。
(1 + 2) * 3 this expression produces 9
Ruby:关系运算符 (Ruby: Relational Operators)
Relational operators are ud for comparisons. They return Boolean values. We compare two values whether they are equal, not equal, less than, greater than, less than or equal to and greater than or equal to.
Relational operators⽤于⽐较。 它们返回Boolean values 。 我们⽐较两个值是否相等,不相等,⼩于,⼤于,⼩于或等于以及⼤于或等于。
== sign is ud. Ud to check whether two numbers are equal or not.
==符号。 ⽤于检查两个数字是否相等。
1 == 1 returns true becau 1 is equal to 1.
< - Less than
< -⼩于
> - Greater than
> -⼤于
<= - Less than or equal to
<= -⼩于或等于
被动态>= - Greater than or equal to
>= -⼤于或等于
true el returns fal
fal. Less than operator checks whether a number is less than the another number, if yes
yes it returns true
Less than operator
Less than or equal to operator checks whether a number is less than to another number and also checks whether a Less than or equal to operator
Greater than number is equal to another number, if any one of the condition is correct it returns true el returns fal. Greater than
greater.
and Greater than or equal to does the same and checks whether it is greater
and Greater than or equal to
fal 。 ⼩于或等于运算符
⼩于或等于运算符检查⼀个数字是否⼩于另⼀
true,否则返回fal
是,则返回true,
⼩于运算符
⼩于运算符检查⼀个数字是否⼩于另⼀个数字,如果是,
⼤于和⼤于或等于进⾏相同操作,并个数字,还检查⼀个数字是否等于另⼀个数字,如果任⼀条件正确,则返回true,否则返回fal。 ⼤于和⼤于或等于进⾏更⼤ 。
检查它是否更⼤
+1 depending on the
-1 or +1
General comparison operator. It returns 0(zero)
0(zero), -1
Another way to compare two values is using General comparison
thisafternoon-1 and operands. If both the values are equal it returns zero
zero, if the first operand is less than the cond operand it returns -1
+1 if the first operand is greater than the cond.
0(零) ,- 11或+1
零 ;如果第⼀个操作数
+1 。 如果两个值相等,则返回零
finish是什么意思常规⽐较运算符 。 根据操作数返回0(零)
⽐较两个值的另⼀种⽅法是使⽤常规⽐较运算符
+1 。
-1 ;如果第⼀个操作数⼤于第⼆个操作数,则返回+1
⼩于第⼆个操作数,则返回-1
The General Comparison operator is <=>(< = >).
转圜
Relational operators can be ud with strings also.
Relational operators也可以与字符串⼀起使⽤。
'Apple' == 'apple' This is becau Ruby is If it is same it returns true. You could e that it returns fal for the operation 'Apple' == 'apple'
General
ca-nsitive and one of the word has Upperca A while the other word has lowerca a. We can also u General comparison operator with strings.
comparison operator
'Apple'=='apple'返回fal,这是因为Ruby区分⼤⼩写,并且其中⼀个单词具
如果相同,则返回true。 您可能会看到它对操作'Apple'=='apple'
General⽐较运算符 。
有Upperca A lowerca a Upperca A⽽另⼀个单词具有lowerca a 。 我们还可以对字符串使⽤General⽐较运算符
-1 becau When comparing 'car'
'car' it returned -1
'cab' with 'car'
'car' and 'car'
'car' it returned 00 since both are equal. When comparing 'cab'
'car'.
'cab' is less than 'r''r' in 'car'
the 3rd letter of the word 'b'
'b' in 'cab'
-1,因为'cab'中
'b'的第三个字母⼩
'cab'中单词'b'
'car'⽐较时,它返回-1,
⽐较“ car”
'cab'与'car'⽐较时,
“ car”和“ car”时
“ car”时 ,由于两者相等,因此返回00 。 当将'cab'
'car'中的 'r'r '' 。
于'car'中的
Ruby:逻辑运算符 (Ruby: Logical Operators)
Logical operators allow you to combine two or more relational expressions and returns Boolean value.
Logical operators允许您组合两个或多个关系表达式并返回Boolean值。
AND operator:
AND运算⼦:
It returns true when all of the expressions are true and returns fal if even one of the expression evaluates to fal. You could u this operator using and or &&.
当所有表达式都为true时,它返回true;即使其中⼀个表达式的计算结果为fal,它也返回fal。 您可以通过and或&&使⽤此运算符。
It returned true for the expression salary == 10 && hours == 40
salary == 10 && hours == 40 becau we've initialized the value of the variables salary and hours as 10 and 40. It will return fal for all other cas.
salary == 10 && hours == 40,它返回true,因为我们已经将变量salary和hours的值初始化为10和40。对于所有其他对于表达式salary == 10 && hours == 40,
情况,它将返回fal。
OR operator:
或运算符:
OR operator returns true when any one condition/expression is true and returns fal only when all of them are fal. You can u this operator using or (or) ||
当任⼀条件/表达式为true时, OR运算符将返回true;仅当所有条件/表达式均为fal时,才返回fal。 您可以使⽤or (或) ||使⽤此运算符
OR operator returned true.
You can e that even when only one expression is true OR
您可以看到,即使只有⼀个表达式为true 或OR
游戏翻译
或OR运算符也返回true。
NOT operator:
transxual
⾮运算符:
NOT operator negates a relational expression. You can u not (or) ! for this operator.
NOT运算符会否定关系表达式。 您可以not使⽤(或) ! 对于这个操作员。
salary == 10 returns true and the not operator negates
In the first expression it returned fal becau the expression salary == 10
true and returns fal. Likewi, the expression salary < 10
salary < 10 returns fal and not operator negates and returns true.
salary <10返
salary == 10返回true,⽽not运算符取反true并返回fal。 同样,表达式salary <10在第⼀个表达式中它返回fal,因为表达式salary == 10
回fal,⽽不是运算符取反,然后返回true。
demonruby 三⽬运算符