The best Ruby Operators Tutorial In 2024, In this tutorial you can learn Ruby arithmetic operators,Ruby comparison,Ruby assignment operator,Ruby parallel assignment,Ruby Bitwise Operators,Ruby logical operators,Ruby ternary operator,Ruby range operator,Ruby defined? Operator,Usage 1,Usage 2,Usage 3,Usage 4,Ruby dot operator "." And the double colon operator "::",Ruby operator precedence,

Ruby Operators

Ruby supports a rich set of operators. Most operators are actually method calls. For example, a + b is interpreted as a. + (B), wherein the variable a + pointing method is invoked, b as a parameter in the method call.

For each operator (+ - * /% ** & | ^ << >> && ||), has a corresponding abbreviation assignment operator (+ = - =, etc.).

Ruby arithmetic operators

Suppose the variable a value of 10, the variable b value of 20, then:

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

Ruby comparison

Suppose the variable a value of 10, the variable b value of 20, then:

运算符描述实例
==检查两个操作数的值是否相等,如果相等则条件为真。 (a == b) 不为真。
!=检查两个操作数的值是否相等,如果不相等则条件为真。 (a != b) 为真。
>检查左操作数的值是否大于右操作数的值,如果是则条件为真。 (a > b) 不为真。
<检查左操作数的值是否小于右操作数的值,如果是则条件为真。 (a < b) 为真。
>=检查左操作数的值是否大于或等于右操作数的值,如果是则条件为真。 (a >= b) 不为真。
<=检查左操作数的值是否小于或等于右操作数的值,如果是则条件为真。 (a <= b) 为真。
<=>联合比较运算符。如果第一个操作数等于第二个操作数则返回 0,如果第一个操作数大于第二个操作数则返回 1,如果第一个操作数小于第二个操作数则返回 -1。 (a <=> b) 返回 -1。
===用于测试case语句的 when 子句内的相等。 (1/en.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

Suppose the variable a value of 10, the variable b value of 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 parallel assignment

Ruby also supports parallel assignment variables. This allows multiple variables can be initialized by a line of Ruby code. E.g:

a = 10
b = 20
c = 30

Using parallel assignment faster Disclaimer:

a, b, c = 10, 20, 30

Parallel assignment when exchanging the values ​​of two variables are also useful:

a, b = b, c

Ruby Bitwise Operators

Bitwise operators acting on the bit, and bit by bit operation.

If assumed that a = 60, and b = 13, now in a binary format, which is 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 Ruby support bitwise operators.

Operators description Examples
& If both exist in two operands, the binary AND operator to copy a result. (A & b) will be 12, that is, 0000 1100
| If present in either operand, the binary OR operator to copy a result. (A | b) will be 61, is 00,111,101
^ If present in one of the operand, but not simultaneously exist in two operands, binary XOR operator a copy to the result. (A ^ b) will be 49, is 00,110,001
~ Twos complement operator is a unary operator, a "flip" position effect. (~ A) will be -61, 1100 0011,2 shall complement form of binary number with sign.
<< Binary left shift operator. The value of the left operand to move left and right operand the specified number of digits. a << 2 will be 240, that is 11.11 million
>> Binary right shift operator. The value of the left operand move right operand the specified number of bits to the right. a >> 2 will be 15, that is, 0000 1111

Ruby logical operators

The following table lists the Ruby support logical operators.

Suppose the variable a value of 10, the variable b value of 20, then:

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

Ruby ternary operator

There is more than one action is called ternary operator. The first computing truth value of the expression, and then decided to implement two behind in a statement based on the result. Conditional operator has the following syntax:

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

Ruby range operator

In Ruby, the sequence is used to create a series of consecutive range value - contains start value, end value (subject to availability) and values ​​between them.

In Ruby, these sequences is the use of "/en" and "/en." range operators to create. Create two o'clock in the form of a range containing the start and end values, the range contains only three o'clock in the form created by the end of the initial value does not contain a value.

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

Ruby defined? Operator

defined? is a special operator, whether in the form of a method call to pass judgment expression is defined. It returns the description string expression, if the expression is undefined returnsnil.

The following are the various operators defined usage?:

Usage 1

defined? variable # If the variable has been initialized True,

E.g:

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

Usage 2

defined? method_call # If the method has been defined, True

E.g:

defined? puts # => "method"
defined? puts (bar) # => nil (undefined here bar)
defined? unpack # => nil (undefined here)

Usage 3

# If the method can be invoked as super user exists, then True
defined? super

E.g:

defined? super # => "super" (if can be called)
defined? super # => nil (if not be invoked)

Usage 4

defined? yield # If the code block passed True,

E.g:

defined? yield # => "yield" (if the transfer block)
defined? yield # => nil (if not pass block)

Ruby dot operator "." And the double colon operator "::"

You can add the module name and underscore to call a module method before the method name. You can use the module name and two colons to reference a constant.

:: Is a unary operator, allowing constants, instance methods and class methods defined within the class or module that can be accessed anywhere in the class or module from the outside.

Remember: In Ruby, classes and methods can also be used as a constant.

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

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

Here are two examples:

MR_COUNT = 0 # Object class defined in the main constants module Foo
  MR_COUNT = 0
  :: MR_COUNT = 1 # to set the global count 1
  MR_COUNT = 2 # set the local count of 2
end
puts MR_COUNT # This is a global constant puts Foo :: MR_COUNT # What is "Foo" local constants 

Second instance:

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

Ruby operator precedence

The following table Based on the priority operator lists all operators.

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

Note: The columnisidentified as the operator method is actually a method, it can be overloaded.

Ruby Operators
10/30