Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Ruby operator


May 12, 2021 Ruby


Table of contents


Ruby operator

Ruby supports a rich set of operators. M ost operators are actually method calls. For example, a plus b is interpreted as a.plus (b), where the method that points to variable a is called, and b is called as an argument to the method call.

For each operator, the number of operations is | ^ << >> && || ), all have a corresponding abbreviated assignment operator ( .

Ruby arithmetic operator

Assuming that the value of variable a is 10 and the value of variable b is 20, then:

运算符 描述 实例
+ 加法 - 把运算符两边的操作数相加 a + b 将得到 30
- 减法 - 把左操作数减去右操作数 a - b 将得到 -10
* 乘法 - 把运算符两边的操作数相乘 a * b 将得到 200
/ 除法 - 把左操作数除以右操作数 b / a 将得到 2
% 求模 - 把左操作数除以右操作数,返回余数 b % a 将得到 0
** 指数 - 执行指数计算 a**b 将得到 10 的 20 次方

Ruby comparison operator

Assuming that the value of variable a is 10 and the value of variable b is 20, then:

运算符 描述 实例
== 检查两个操作数的值是否相等,如果相等则条件为真。 (a == b) 不为真。
!= 检查两个操作数的值是否相等,如果不相等则条件为真。 (a != b) 为真。
> 检查左操作数的值是否大于右操作数的值,如果是则条件为真。 (a > b) 不为真。
< 检查左操作数的值是否小于右操作数的值,如果是则条件为真。 (a < b) 为真。
>= 检查左操作数的值是否大于或等于右操作数的值,如果是则条件为真。 (a >= b) 不为真。
<= 检查左操作数的值是否小于或等于右操作数的值,如果是则条件为真。 (a <= b) 为真。
<=> 联合比较运算符。如果第一个操作数等于第二个操作数则返回 0,如果第一个操作数大于第二个操作数则返回 1,如果第一个操作数小于第二个操作数则返回 -1。 (a <=> b) 返回 -1。
=== 用于测试 case 语句的 when 子句内的相等。 (1...10) === 5 返回 true。
.eql? 如果接收器和参数具有相同的类型和相等的值,则返回 true。 1 == 1.0 返回 true,但是 1 .eql? (1.0) 返回 false。
equal? 如果接收器和参数具有相同的对象 id,则返回 true。 如果 aObj 是 bObj 的副本,那么 aObj == bObj 返回 true,a .equal? bObj 返回 false,但是 a .equal? aObj 返回 true。

Ruby assignment operator

Assuming that the value of variable a is 10 and the value of variable b is 20, then:

运算符 描述 实例
= 简单的赋值运算符,把右操作数的值赋给左操作数 c = a + b 将把 a + b 的值赋给 c
+= 加且赋值运算符,把右操作数加上左操作数的结果赋值给左操作数 c += a 相当于 c = c + a
-= 减且赋值运算符,把左操作数减去右操作数的结果赋值给左操作数 c -= a 相当于 c = c - a
*= 乘且赋值运算符,把右操作数乘以左操作数的结果赋值给左操作数 c *= a 相当于 c = c * a
/= 除且赋值运算符,把左操作数除以右操作数的结果赋值给左操作数 c /= a 相当于 c = c / a
%= 求模且赋值运算符,求两个操作数的模赋值给左操作数 c %= a 相当于 c = c % a
**= 指数且赋值运算符,执行指数计算,并赋值给左操作数 c **= a 相当于 c = c ** a

Ruby is assigned in parallel

Ruby also supports parallel assignments of variables. T his allows multiple variables to be initialized through a single line of Ruby code. For example:

a = 10
b = 20
c = 30

Parallel assignments allow you to declare faster:

a, b, c = 10, 20, 30

Parallel assignments are also useful when exchanging values for two variables:

a, b = b, c

Ruby bit operator

Bit operators act on bits and perform operations bit by bit.

Suppose that if a is 60 and b is 13, they are now in binary format, as follows:

a = 0011 1100

b = 0000 1101

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

a&b = 0000 1100

a|b = 0011 1101

a^b = 0011 0001

~a = 1100 0011

The following table lists the bit operators supported by Ruby.

运算符 描述 实例
& 如果同时存在于两个操作数中,二进制 AND 运算符复制一位到结果中。 (a & b) 将得到 12,即为 0000 1100
| 如果存在于任一操作数中,二进制 OR 运算符复制一位到结果中。 (a | b) 将得到 61,即为 0011 1101
^ 如果存在于其中一个操作数中但不同时存在于两个操作数中,二进制异或运算符复制一位到结果中。 (a ^ b) 将得到 49,即为 0011 0001
~ 二进制补码运算符是一元运算符,具有"翻转"位效果。 (~a ) 将得到 -61,即为 1100 0011,2 的补码形式,带符号的二进制数。
<< 二进制左移运算符。左操作数的值向左移动右操作数指定的位数。 a << 2 将得到 240,即为 1111 0000
>> 二进制右移运算符。左操作数的值向右移动右操作数指定的位数。 a >> 2 将得到 15,即为 0000 1111

Ruby logical operator

The following table lists the logical operators that Ruby supports.

Assuming that the value of variable a is 10 and the value of variable b is 20, then:

运算符 描述 实例
and 称为逻辑与运算符。如果两个操作数都为真,则条件为真。 (a and b) 为真。
or 称为逻辑或运算符。如果两个操作数中有任意一个非零,则条件为真。 (a or b) 为真。
&& 称为逻辑与运算符。如果两个操作数都非零,则条件为真。 (a && b) 为真。
|| 称为逻辑或运算符。如果两个操作数中有任意一个非零,则条件为真。 (a || b) 为真。
! 称为逻辑非运算符。用来逆转操作数的逻辑状态。如果条件为真则逻辑非运算符将使其为假。 !(a && b) 为假。
not 称为逻辑非运算符。用来逆转操作数的逻辑状态。如果条件为真则逻辑非运算符将使其为假。 not(a && b) 为假。

Ruby 3 operator

There is more than one operation called a thyme operator. T he first evaluates the true and false values of the expression, and then, based on this result, decides to execute one of the last two statements. The syntax of the conditional operator is as follows:

运算符 描述 实例
? : 条件表达式 如果条件为真 ? 则值为 X : 否则值为 Y

Ruby range operator

In Ruby, the sequence range is used to create a series of consecutive values - including the start value, the end value (as the case may be) and the values between them.

In Ruby, these sequences are used "..." and "..." R ange operator to create. The range created in two-point form contains the start and end values, and the range created in the three-point form contains only the start value and not the end value.

运算符 描述 实例
.. 创建一个从开始点到结束点的范围(包含结束点) 1..10 创建从 1 到 10 的范围
... 创建一个从开始点到结束点的范围(不包含结束点) 1...10 创建从 1 到 9 的范围

Ruby defined? Operator

defined? i s a special operator that determines whether a passed expression is defined in the form of a method call. It returns a description string of the expression and nil if the expression is not defined.

Here's the defined? Various uses of operators:

Usage 1

defined? variable # 如果 variable 已经初始化,则为 True

For example:

foo = 42
defined? foo    # => "local-variable"
defined? $_     # => "global-variable"
defined? bar    # => nil(未定义)

Usage 2

defined? method_call # 如果方法已经定义,则为 True

For example:

defined? puts        # => "method"
defined? puts(bar)   # => nil(在这里 bar 未定义)
defined? unpack      # => nil(在这里未定义)

Usage 3

# 如果存在可被 super 用户调用的方法,则为 True
defined? super

For example:

defined? super     # => "super"(如果可被调用)
defined? super     # => nil(如果不可被调用)

Usage 4

defined? yield   # 如果已传递代码块,则为 True

For example:

defined? yield    # => "yield"(如果已传递块)
defined? yield    # => nil(如果未传递块)

Ruby point operator "." and dual colon operator "::"

You can call a module method by adding the module name and an underscore before the method name. You can use the module name and two colons to refer to a constant.

:: is a binary operator that allows constants, instance methods, and class methods to be defined within a class or module and can be accessed from anywhere outside the class or module.

Keep in mind that in Ruby, classes and methods can also be treated as constants.

You only need to add the :: prefix before the constant name of the expression to return the appropriate class or module object.

If the prefix expression is not used, the main Object class is used by default.

Here are two examples:

MR_COUNT = 0        # 定义在主 Object 类上的常量
module Foo
  MR_COUNT = 0
  ::MR_COUNT = 1    # 设置全局计数为 1
  MR_COUNT = 2      # 设置局部计数为 2
end
puts MR_COUNT       # 这是全局常量
puts Foo::MR_COUNT  # 这是 "Foo" 的局部常量 

The second example:

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

The priority of the Ruby operator

The following table lists all operators from highest to lowest by their priority.

方法 运算符 描述
:: 常量解析运算符
[ ] [ ]= 元素引用、元素集合
** 指数
! ~ + - 非、补、一元加、一元减(最后两个的方法名为 +@ 和 -@)
* / % 乘法、除法、求模
+ - 加法和减法
>> << 位右移、位左移
& 位与
^ | 位异或、位或
<= < > >= 比较运算符
<=> == === != =~ !~ 相等和模式匹配运算符(!= 和 !~ 不能被定义为方法)
&& 逻辑与
|| 逻辑或
.. ... 范围(包含、不包含)
? : 三元 if-then-else
= %= { /= -= += |= &= >>= <<= *= &&= ||= **= 赋值
defined? 检查指定符号是否已定义
not 逻辑否定
or and 逻辑组成

Note: Operators identified as Yes in the method column are actually methods and can therefore be overloaded.