# Ruby Operators

Ruby supports a rich set of operators. Most operators are actually method calls. For example, a + b is interpreted as a.+(b), where the + method pointing to the variable a is called and b is the argument to the method call.

For each operator (+ - * / % ** & | ^ << >> && ||), there is a corresponding abbreviation assignment operator (+= - = and so on).

## Ruby arithmetic operators

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

Operators | Description | Instances |
---|---|---|

+ | Addition - Add the operands on both sides of the operator | a + b will get 30 |

- | Subtraction - Subtract the left operand from the right operand | a - b will get -10 |

* | Multiplication - Multiply the operands on both sides of the operator | a * b will get 200 |

/ | Division - Divide the left operand by the right operand | b / a will get 2 |

% | Modification - Divide the left operand by the right operand and return the remainder | b % a will get 0 |

** | Index - Execution Index Calculation | a**b will get 10 20th power |

## Ruby comparison operator

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

Operators | Description | Instances |
---|---|---|

== | Checks if the values of the two operands are equal, and if they are equal, the condition is true. | (a == b) is not true. |

!= | Checks if the values of the two operands are equal. If they are not equal, the condition is true. | (a != b) is true. |

> | Checks if the value of the left operand is greater than the value of the right operand, and if so, the condition is true. | (a > b) Not true. |

< | Checks if the value of the left operand is less than the value of the right operand, and if so the condition is true. | (a < b) is true. |

>= | Checks if the value of the left operand is greater than or equal to the value of the right operand, and if so, the condition is true. | (a >= b) is not true. |

<= | Checks if the value of the left operand is less than or equal to the value of the right operand, and if so the condition is true. | (a <= b) is true. |

<=> | Joint comparison operators. Returns 0 if the first operand is equal to the second operand, and returns 1 if the first operand is greater than the second operand, and -1 if the first operand is less than the second operand. | (a <=> b) returns -1. |

=== | is used to test equality within the when clause of the case statement. | (1...10) === 5 returns true. |

.eql? | Returns true if the receiver and parameters have the same type and equal value. | 1
== 1.0 returns true, but 1.eql?(1.0) returns false. |

equal? | Returns true if the receiver and parameters have the same object id. | If aObj is a copy of bObj, then aObj == bObj returns true, a.equal?bObj returns false, but a.equal?aObj Returns true. |

## Ruby assignment operator

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

Operators | Description | Instances |
---|---|---|

= | Simple assignment operator assigns the value of the right operand to the left operand | c = a + b will assign the value of a + b to c |

+= | Add and assign the operator, assign the result of the right operand plus the left operand to the left operand | c += a is equivalent to c = c + a |

-= | minus the assignment operator, assigning the left operand minus the result of the right operand to the left operand | c -= a is equivalent to c = c - a |

*= | Multiply and assign operator, assign the result of multiplying the right operand to the left operand to the left operand | c *= a is equivalent to c = c * a |

/= | In addition to the assignment operator, the result of dividing the left operand by the right operand is assigned to the left operand | c /= a is equivalent to c = c / a |

%= | modulo and assignment operator, find the modulo assignment of two operands to the left operand | c %= a is equivalent to c = c % a< /td> |

**= | index and assignment operator, perform exponential calculation, and assign to left operand | c **= a is equivalent to c = c ** a< /td> |

## Ruby parallel assignment

Ruby also supports parallel assignment of variables. This allows multiple variables to be initialized with one line of Ruby code. For example:

Use parallel assignment to declare faster:

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

## Ruby bit operator

Thebitwise operator acts on bits and performs operations bit by bit.

Assume that if a = 60 and b = 13, now in binary format, they are 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 bitwise operators supported by Ruby.

Operators | Description | Instances |
---|---|---|

& | If both operands exist in both operands, the binary AND operator copies one bit into the result. | (a & b) will get 12, which is 0000 1100 |

| | If present in any operand, the binary OR operator copies one bit into the result. | (a | b) will get 61, which is 0011 1101 |

^ | If it exists in one of the operands but not in both operands, the binary XOR operator copies one bit into the result. | (a ^ b) will get 49, which is 0011 0001 |

~ | The two's complement operator is a unary operator with a "flip" bit effect, ie 0 becomes 1, and 1 becomes 0. | (~a ) will get -61, which is 1100 0011, a complement of a signed binary number. |

<< | Binary left shift operator. The value of the left operand moves to the left by the number of digits specified by the right operand. | a << 2 will get 240, which is 1111 0000 |

>> | Binary right shift operator. The value of the left operand moves to the right by the number of digits specified by the right operand. | a >> 2 will get 15, which is 0000 1111 |

## Ruby logical operators

The following table lists the logical operators supported by Ruby.

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

Operators | Description | Instances |
---|---|---|

and | is called a logical AND operator. The condition is true if both operands are true. | (a and b) is true. |

or | is called a logical OR operator. The condition is true if either of the two operands is non-zero. | (a or b) is true. |

&& | is called a logical AND operator. The condition is true if both operands are non-zero. | (a && b) is true. |

|| | is called a logical OR operator. The condition is true if either of the two operands is non-zero. | (a || b) is true. |

! | is called a logical non-operator. The logical state used to reverse the operands. A logical non-operator will make it false if the condition is true. | !(a && b) is false. |

not | is called a logical non-operator. The logical state used to reverse the operands. A logical non-operator will make it false if the condition is true. | not(a && b) is false. |

## Ruby ternary operator

There is more than one operation called the ternary operator. The first evaluates the true and false values of the expression, and then decides to execute one of the two statements following the result. The syntax of the conditional operator is as follows:

Operators | Description | Instances |
---|---|---|

? : | conditional expression | If the condition is true, then the value is X : otherwise the value is Y |

## Ruby range operator

In Ruby, sequence ranges are used to create a series of consecutive values - including start values, end values (as appropriate), and values between them.

In Ruby, these sequences are created using the ".." and "..." range operators. The two-point form creates a range of start and end values, and the three-point form creates a range that only contains the start value and no end value.

Operators | Description | Instances |
---|---|---|

.. | Create a range from the start point to the end point (including the end point) | 1..10 Create a range from 1 to 10 |

... | Create a range from the start point to the end point (without the end point) | 1...10 Create a range from 1 to 9 |

## Ruby defined? operator

defined? is a special operator that determines whether the passed expression is defined in the form of a method call. It returns the description string of the expression, or *nil* if the expression is undefined.

The following are the various uses of the defined operator:

## Usage 1

Example:

## Usage 2

Example:

## Usage 3

Example:

## Usage 4

Example:

## Ruby dot operator "." and double colon operator "::"

You can call methods in a class or module by prefixing the method name with the class or module name and .. You can refer to a constant in a class or module using the class or module name and two colons ::.

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

**Remember:** In Ruby, classes and methods can also be treated as constants.

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

If the expression before :: is a class or module name, return the corresponding constant value in that class or module; if : : There is no prefix expression before, returning the corresponding constant value in the main Object class. .

The following are two examples:

Second instance:

## Priority of the Ruby operator

The following table lists all operators from high to low according to their priority.

Method | Operator | Description |
---|---|---|

Yes | :: | Constant Parsing Operator |

Yes | [ ] [ ]= | Element reference, element collection |

Yes | ** | index |

Yes | ! ~ + - | Non, Complement, One Dollar Plus, One Dollar Subtraction (the last two methods are named +@ and -@) |

Yes | * / % | multiplication, division, modulo |

Yes | + - | Addition and Subtraction |

Yes | >> << | bit shifts right and left shifts |

Yes | & | bit and |

Yes | ^ | | XOR, bit or |

Yes | <= < > >= | Compare operator |

Yes | <=> == === != =~ !~ | equality and pattern matching operators (!= and !~ cannot be defined as methods) |

&& | Logic and | |

|| | Logic or | |

.. ... | scope (included, not included) | |

? : | ternary if-then-else | |

= %= { /= -= += |= &= >>= <<= *= &&= ||= **= | assignment | |

defined? | Check if the specified symbol is defined | |

not | Logical negation | |

or and | Logical composition |

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