Centos系统下载大全 | Redhat系统下载大全 | Windows2012系统下载大全 | Windows2008系统下载大全| CMS教程 | 网站地图 51运维网-专注Linux/Unix系统安全运维!
当前位置:51运维网 > 编程 > Ruby > 正文

Ruby运算符

时间:2014-08-20 23:47 来源:未知 作者:www.51ou.com 阅读:
Ruby支持一系列丰富的运算符的一个现代化的语言。大多数运算符实际上是方法调用。例如,a + b的被解释为a,
+(b)变量引用的对象的方法被称为一个用b作为它的参数。

对于每个运算符 (+ - * / % ** & | ^ << >> && ||), 有相应的赋值运算符缩写形式 (+= -= 等)

Ruby算术运算符:

假设变量a=10,变量b=20:

操作符 描述 例子
+ Addition - Adds values on either side of the operator a + b = 30
- Subtraction - Subtracts right hand operand from left hand operand a - b = -10
* Multiplication - Multiplies values on either side of the operator a * b = 200
/ Division - Divides left hand operand by right hand operand b / a = 2
% Modulus - Divides left hand operand by right hand operand and returns remainder b % a = 0
** Exponent - Performs exponential (power) calculation on operators a**b = 10 to the power 20

Ruby比较操作符:

假设变量a=10,变量b=20:

操作符 描述 例子
== 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 a case 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. if aObj is duplicate of bObj then aObj == bObj is true, a.equal?bObj is false but a.equal?aObj is true.

Ruby赋值运算符:

假设变量a=10,变量b=20:

操作符 描述 例子
= 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 c **= a is equivalent to c = c ** a

Ruby并行赋值:

Ruby还支持并行赋值的变量。这使得多个一行Ruby代码来初始化变量。例如:

a = 10
b = 20
c = 30  

需要更迅速声明,使用并行赋值:

a, b, c = 10, 20, 30  

并行赋值交换两个变量的值也是有用的:

a, b = b, c  

Ruby位运算符:

位运算符位和位操作执行位。

假设当a =60和b=13;现在以二进制格式将如下:

a = 0011 1100

b = 0000 1101

-----------------

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a  = 1100 0011

支持Ruby语言的位运算符

运算符 描述 实例
& Binary AND Operator copies a bit to the result if it exists in both operands. (a & b) will give 12 which is 0000 1100
| Binary OR Operator copies a bit if it exists in either operand. (a | b) will give 61 which is 0011 1101
^ Binary XOR Operator copies the bit if it is set in one operand but not both. (a ^ b) will give 49 which is 0011 0001
~ Binary Ones Complement Operator is unary and has the efect of 'flipping' bits. (~a ) will give -61 which is 1100 0011 in 2's complement form due to a signed binary number.
<< Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. a << 2 will give 240 which is 1111 0000
>> Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. a >> 2 will give 15 which is 0000 1111

Ruby逻辑运算符:

支持Ruby语言的逻辑运算符

假设变量a=10,变量b=20:

运算符 描述 例子
and Called Logical AND operator. If both the operands are true then then condition becomes true. (a and b) is true.
or Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. (a or b) is true.
&& Called Logical AND operator. If both the operands are non zero then then condition becomes true. (a && b) is true.
|| Called Logical OR Operator. If any of the two operands are non zero then then condition becomes true. (a || b) is true.
! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. !(a && b) is false.
not Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. not(a && b) is false.

Ruby三元运算符:

还有一个运算符称为三元运算符。这首先计算一个表达式为true或false值,然后执行一个计算结果来决定两个语句的哪一个。条件运算符的语法如下:

运算符 描述 例子
? : Conditional Expression If Condition is true ? Then value X : Otherwise value Y

Ruby范围运算符:

Ruby中的序列范围是用来创建连续值 - 组成了开始值/结束值,并在两者之间的值的范围内。

在Ruby中,创建这些序列使用“..”和“...”范围运算符。这两个点的形式建立一个包容性的范围,而三个点的形式创建了一个范围,不包括指定的最大值。

运算符 描述 例子
.. Creates a range from start point to end point inclusive 1..10 Creates a range from 1 to 10 inclusive
... Creates a range from start point to end point exclusive 1...10 Creates a range from 1 to 9

感谢您对【51运维网 http://www.51ou.com/】的支持,我们为您免费提供《Ruby运算符》技术文章,《Ruby运算符》详细使用和说明,有时《Ruby运算符》可能不完善、敬请谅解!如果《Ruby运算符》有错误请给我们留言,我们将尽快修复文章错误,如果您觉得本站不错,请分享给周围的朋友!谢谢!

顶一下
(0)
0%
踩一下
(0)
0%
上一篇:Ruby变量
下一篇:Ruby注释
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
验证码:点击我更换图片