Ruby运算符

本文详细介绍了Ruby语言中的各种运算符,包括算术运算符、比较运算符和赋值运算符,并提供了具体的使用示例。

描述

Ruby支持一套丰富的运算符,如你所期望从现代语言。大多数运营商实际上是方法调用。例如,被解释为A + B。+(B),其中变量所指对象+方法被称为与B的参数。

Ruby支持一套丰富的运算符,如你所期望从现代语言。大多数运营商实际上是方法调用。例如,被解释为A + B。+(B),其中变量所指对象+方法被称为与B的参数。

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

Ruby算术运算符:


假设变量拥有10和变量b则持有20:
 
OperatorDescriptionExample
+Addition - Adds values on either side of the operatora + b will give 30
-Subtraction - Subtracts right hand operand from left hand operanda - b will give -10
*Multiplication - Multiplies values on either side of the operatora * b will give 200
/Division - Divides left hand operand by right hand operandb / a will give 2
%Modulus - Divides left hand operand by right hand operand and returns remainderb % a will give 0
**Exponent - Performs exponential (power) calculation on operatorsa**b will give 10 to the power 20

Ruby比较操作符:


假设变量拥有10和变量b则持有20:
 
OperatorDescriptionExample
==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:

OperatorDescriptionExample
=Simple assignment operator, Assigns values from right side operands to left side operandc = 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 operandc += 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 operandc -= 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 operandc *= 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 operandc /= a is equivalent to c = c / a
%=Modulus AND assignment operator, It takes modulus using two operands and assign the result to left operandc %= a is equivalent to c = c % a
**=Exponent AND assignment operator, Performs exponential (power) calculation on operators and assign value to the left operand

转载于:https://my.oschina.net/ruby65/blog/72832

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值