假设变量a=10,变量b=20,则-
| Operator | Remark | Example |
|---|---|---|
| + | 相加 | a + b=30 |
| - | 相减 | a- b=-10 |
| * | 相乘 | a * b=200 |
| / | 相除 | b/a=2 |
| % | 取模 | b % a=0 |
| ** | 指数 | a**b=10 to the power 20 |
假设变量a=10,变量b=20,则-
| Operator | Remark | Example |
|---|---|---|
| == | 相等 | (a == b) is not true. |
| != | 不相等 | (a != b) is true. |
| > | 大于 | (a > b) is not true. |
| < | 小于 | (a < b) is true. |
| >= | 大于或等于 | (a >= b) is not true. |
| <= | 小于或等于 | (a <= b) is true. |
| <=> | 组合比较运算符 | (a <=> b) returns-1. |
| === | 用于在case语句的when子句中测试相等性。 | (1...10) === 5 returns true. |
| .eql? | 具有相同的类型和相同的值,则为true。 | 1 == 1.0 returns true, but 1.eql?(1.0) is false. |
| equal? | 具有相同的对象ID,则为true。 | 如果aObj是bObj的副本则aObj == bObj为true,则aObj.equal?bObj为false |
假设变量a=10,变量b=20,则-
| Operator | Remark | Example |
|---|---|---|
| = | 赋值 | c=a + b will assign the value of a + b into c |
| += | 相加 | c += a is equivalent to c=c + a |
| -= | 相减 | c-= a is equivalent to c=c- a |
| *= | 相乘 | c *= a is equivalent to c=c * a |
| /= | 相除 | c/= a is equivalent to c=c/a |
| %= | 取模 | c %= a is equivalent to c=c % a |
| **= | 指数 | c **= a is equivalent to c=c ** a |
a=10 b=20 c=30
使用并行分配可以更快地声明-
a, b, c=10, 20, 30
a, b=b, c
按位运算符对位进行运算并执行逐位运算。
假设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语言支持以下按位运算符。
| Operator | Remark | Example |
|---|---|---|
| & | and | (a & b)=12, which is 0000 1100 |
| | | or | (a | b)=61, which is 0011 1101 |
| ^ | XOR | (a ^ b)=49, which is 0011 0001 |
| ~ | 非 | (~a )=-61, which is 1100 0011 in 2's complement form due to a signed binary number. |
| << | 左移 | a << 2=240, which is 1111 0000 |
| >> | 右移 | a >> 2=15, which is 0000 1111 |
Ruby语言支持以下逻辑运算符,假设变量 a =10,变量 b =20,则-
| Operator | 描述 | Example |
|---|---|---|
| and | 称为逻辑AND运算符。 如果两个操作数都为true,则条件为true。 | (a and b) is true. |
| or | 称为逻辑或运算符。 如果两个操作数中的任何一个都不为零,则条件变为true。 | (a or b) is true. |
| && | 称为逻辑AND运算符。 如果两个操作数都不为零,则条件变为true。 | (a && b) is true. |
| || | 称为逻辑或运算符。 如果两个操作数中的任何一个都不为零,则条件变为true。 | (a || b) is true. |
| ! | 称为逻辑非运算符。 如果条件为真,则逻辑非运算符将为假。 | !(a && b) is false. |
| not | 称为逻辑非运算符。 如果条件为真,则逻辑非运算符将为假。 | not(a && b) is false. |
还有一个运算符称为三元运算符。它首先对表达式的真值或假值求值,然后根据求值输出执行两个给定语句之一。条件运算符具有以下语法-
| Operator | Remark | Example |
|---|---|---|
| ? : | 条件表达式 | 如果条件为true?然后取值X:否则取值Y |
Ruby中的序列范围用于创建一系列连续值-由开始值,结束值以及介于两者之间的值范围组成。
在Ruby中,这些序列是使用" .."和" ..."范围运算符创建的。两点形式创建一个包含范围,而三点形式创建一个排除指定高值的范围。
| Operator | Remark | Example |
|---|---|---|
| .. | 创建一个从起点到终点的范围。 | 1..10创建一个介于1到10之间的范围。 |
| ... | 创建一个从起点到终点的范围。 | 1 ... 10创建1到9的范围。 |
defined?是一个特殊的运算符,采用方法调用的形式来确定是否定义了所传递的表达式。它返回表达式的描述字符串,如果未定义表达式,则返回 nil
用法1
defined? variable # True if variable is initialized
如
foo=42 defined? foo # => "local-variable" defined? $_ # => "global-variable" defined? bar # => nil (undefined)
用法2
defined? method_call # True if a method is defined
如
defined? puts # => "method" defined? puts(bar) # => nil (bar is not defined here) defined? unpack # => nil (not defined here)
用法3
# True if a method exists that can be called with super user defined? super
如
defined? super # => "super" (if it can be called) defined? super # => nil (if it cannot be)
用法4
defined? yield # True if a code block has been passed
如
defined? yield # => "yield" (if there is a block passed) defined? yield # => nil (if there is no block)
您可以通过在模块名称前加上模块名称和句点来调用模块方法,并使用模块名称和两个冒号引用常量。
::是一元运算符,它允许:在类或模块内定义的常量,方法和类方法可以从类或模块外部的任何位置进行访问。
这是两个示例-
MR_COUNT=0 # constant defined on main对象class module Foo MR_COUNT=0 ::MR_COUNT=1 # set global count to 1 MR_COUNT=2 # set local count to 2 end puts MR_COUNT # this is the global constant puts Foo::MR_COUNT # this is the local "Foo" constant
第二个示例
CONST=' out there' class Inside_one CONST=proc {' in there'} def where_is_my_CONST ::CONST + ' inside one' end end class Inside_two CONST=' inside two' def where_is_my_CONST CONST end end puts Inside_one.new.where_is_my_CONST puts Inside_two.new.where_is_my_CONST puts Object::CONST + Inside_two::CONST puts Inside_two::CONST + CONST puts Inside_one::CONST puts Inside_one::CONST.call + Inside_two::CONST
祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)