描述
Ruby支持一套丰富的运算符,如你所期望从现代语言。大多数运营商实际上是方法调用。例如,被解释为A + B。+(B),其中变量所指对象+方法被称为与B的参数。
Ruby支持一套丰富的运算符,如你所期望从现代语言。大多数运营商实际上是方法调用。例如,被解释为A + B。+(B),其中变量所指对象+方法被称为与B的参数。
对于每个运算符(+ - * /%**&| ^<< >>&&| |),有相应的赋值运算符缩写形式(+= - =等)
Ruby算术运算符:
假设变量拥有10和变量b则持有20:
Operator | Description | Example |
---|---|---|
+ | Addition - Adds values on either side of the operator | a + b will give 30 |
- | Subtraction - Subtracts right hand operand from left hand operand | a - b will give -10 |
* | Multiplication - Multiplies values on either side of the operator | a * b will give 200 |
/ | Division - Divides left hand operand by right hand operand | b / a will give 2 |
% | Modulus - Divides left hand operand by right hand operand and returns remainder | b % a will give 0 |
** | Exponent - Performs exponential (power) calculation on operators | a**b will give 10 to the power 20 |
Ruby比较操作符:
假设变量拥有10和变量b则持有20:
Operator | Description | Example |
---|---|---|
== | Checks if the value of two operands are equal or not, if yes then condition becomes true. | (a == b) is not true. |
!= | Checks if the value of two operands are equal or not, if values are not equal then condition becomes true. | (a != b) is true. |
> | Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. | (a > b) is not true. |
< | Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. | (a < b) is true. |
>= | Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. | (a >= b) is not true. |
<= | Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. | (a <= b) is true. |
<=> | Combined comparison operator. Returns 0 if first operand equals second, 1 if first operand is greater than the second and -1 if first operand is less than the second. | (a <=> b) returns -1. |
=== | Used to test equality within a when clause of acase statement. | (1...10) === 5 returns true. |
.eql? | True if the receiver and argument have both the same type and equal values. | 1 == 1.0 returns true, but 1.eql?(1.0) is false. |
equal? | True if the receiver and argument have the same object id. | 1 == 1.0 returns true, but 1.eql?(1.0) is false. |
Ruby分配运算符:
假设变量拥有10和变量b则持有20:
Operator | Description | Example |
---|---|---|
= | Simple assignment operator, Assigns values from right side operands to left side operand | c = a + b will assigne value of a + b into c |
+= | Add AND assignment operator, It adds right operand to the left operand and assign the result to left operand | c += a is equivalent to c = c + a |
-= | Subtract AND assignment operator, It subtracts right operand from the left operand and assign the result to left operand | c -= a is equivalent to c = c - a |
*= | Multiply AND assignment operator, It multiplies right operand with the left operand and assign the result to left operand | c *= a is equivalent to c = c * a |
/= | Divide AND assignment operator, It divides left operand with the right operand and assign the result to left operand | c /= a is equivalent to c = c / a |
%= | Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operand | c %= a is equivalent to c = c % a |
**= | Exponent AND assignment operator, Performs exponential (power) calculation on operators and assign value to the left operand |