Ruby - Operators
Operators are used to perform operation on a single operand or two operands. Operators in Ruby can be categorized as follows:
- Arithmetic operators
- Assignment operators
- Comparison operators
- Logical operators
- Bitwise operators
- Miscellaneous operators
Ruby Arithmetic operators
Arithmetic operators are used to perform arithmetic operations on two operands.
Operator | Name | Description |
---|---|---|
+ | Addition | Add two values |
- | Subtraction | Subtract one value from another |
* | Multiplication | Multiply two values |
/ | Division | Divide one value by another |
% | Modulo | Returns remainder of division operation |
** | Exponent / Power | Returns first operand raised to the power of second operand |
Example
Ruby Assignment operators
Assignment operators are used to assign values of right hand side expression to left hand side operand.
Operator | Expression | Equivalent to | Description |
---|---|---|---|
= | a = 5 | a = 5 | Example |
+= | a += b | a = a + b | |
-= | a -= b | a = a - b | |
*= | a *= b | a = a * b | |
/= | a /= b | a = a / b | |
%= | a %= b | a = a % b | |
**= | a **= b | a = a ** b | |
&= | a &= b | a = a & b | More Info |
|= | a |= b | a = a | b | More Info |
^= | a ^= b | a = a ^ b | More Info |
>>= | a >>= b | a = a >> b | More Info |
<<= | a <<= b | a = a << b | More Info |
Ruby Comparison operators
Comparison operators are used to compare values of two operands. It returns true when values matches and returns false when values does not match.
Operator | Description |
---|---|
== | Equal: Checks the values of two operands and returns true if they are same. |
!= | Not equal: Checks the values of two operands and returns true if they are not same. |
=== | Case equality: A === B, Checks if B is a member of the set of A. |
> | Greater than: Checks the values of two operands and returns true if the value of first operand is greater than the value of second operand. |
< | Less than: Checks the values of two operands and returns true if the value of first operand is less than the value of second operand. |
>= | Greater than or equal to: Checks the values of two operands and returns true if the value of first operand is greater than or equal to the value of second operand. |
<= | Less than or equal to: Checks the values of two operands and returns true if the value of first operand is less than or equal to the value of second operand. |
<=> | Combined Comparison Operator: Checks the values of two operands and returns values based on the values of two operands:
|
.eql? | Equal: Checks the values and datatypes of two operands and returns true if they are same. |
Example
Ruby Logical operators
Logical operators are used to combine two or more conditions.
Operator | Name | Description |
---|---|---|
and | Logical AND | Returns true when all conditions are true |
&& | ||
or | Logical OR | Returns true when any of the conditions is true |
|| | ||
not | Logical NOT | Returns true when given conditions is not true |
! |
More Info
Ruby Bitwise operators
Bitwise operators are used to perform bitwise operations on two operands.
Operator | Name | Description | More Info |
---|---|---|---|
& | AND | Returns 1 if both bits at the same position in both operands are 1, else returns 0. | More Info |
| | OR | Returns 1 if one of two bits at the same position in both operands is 1, else returns 0. | More Info |
^ | XOR | Returns 1 if only one of two bits at the same position in both operands is 1, else returns 0. | More Info |
~ | NOT | Reverse all the bits. | More Info |
>> | Right shift | The left operand is moved right by the number of bits present in the right operand. | More Info |
<< | Left shift | The left operand value is moved left by the number of bits present in the right operand. | More Info |
Ruby Miscellaneous operators
The table below describes other operators supported by Ruby:
Operator | Description |
---|---|
ternary operator (?:) | Returns one of the two values based on value of boolean expression. |
range operator (..) | Creates a range from start point to end point inclusive. |
range operator (...) | Creates a range from start point to end point exclusive. |
Ruby Operators Precedence
Operator precedence (order of operations) is a collection of rules that reflect conventions about which procedures to perform first in order to evaluate a given expression.
For example, multiplication has higher precedence than addition. Thus, the expression 1 + 2 × 3 is interpreted to have the value 1 + (2 × 3) = 7, and not (1 + 2) × 3 = 9. When exponent is used in the expression, it has precedence over both addition and multiplication. Thus 3 + 52 = 28 and 3 × 52 = 75.
The following table lists the precedence and associativity of Ruby operators. Operators are listed top to bottom, in descending precedence. Operators with higher precedence are evaluated before operators with relatively lower precedence.
Precedence | Operator | Description |
---|---|---|
21 | ! | Logical NOT |
~ | Bitwise NOT | |
+a | Unary plus | |
20 | ** | Exponential operator |
19 | -a | Unary minus |
18 | * / % | Multiplication, Division, Remainder |
17 | + - | Addition, Subtraction |
16 | << >> | Bitwise left shift and right shift |
15 | & | Bitwise AND |
14 | | | Bitwise OR |
^ | Bitwise XOR | |
13 | < <= > >= | Less than, Less than or equal, Greater than, and Greater than or equal |
12 | == != | Equality and Inequality |
=== <=> | Case equality, Combined Comparison Operator | |
=~ !~ | Pattern matching operators | |
11 | && | Logical AND |
10 | || | Logical OR |
9 | .. | Range creation operators |
... | ||
8 | a?b:c | ternary (conditional) operator |
7 | modifier-rescue | Exception-handling modifier |
6 | = | Direct assignment |
+= -= *= /= %= **= | Compound assignment by sum, difference, product, quotient, remainder and exponential | |
<<= >>= | Compound assignment by Bitwise left shift and right shift | |
&= ^= |= | Compound assignment by Bitwise AND, XOR and OR | |
5 | defined? | Test variable definition and type |
4 | not | Logical NOT |
3 | and | Logical AND |
or | Logical OR | |
2 | modifier-if modifier-unless modifier-while modifier-until | Conditional and loop modifiers |
1 | { } | blocks |