Operators

An operator is a symbol which has special meaning and performs an operation on single or multiple operands like addition, substraction etc. Ruby provides rich set of in-built operators.

Types of Operators in Ruby

1. Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations on operands.

OperatorDescriptionExample
+Used to perform Addition8+2 = 10
-Used to perform Subtraction12-2 = 10
*Used to perform Multiplication5*2 = 10
/Used to perform Division and returns float values10/3 = 3.3333
%Used to return Remainder40%10 = 0
**Used to raise power of another operandx ** y = x to the power of y

2. Comparison Operators

Comparison operators are used to compare two operands.

OperatorDescriptionUsage
==Is equal tox == y
!=Not equal to!=x
>Greater thanx > y
>=Greater than or equal tox >= y
<Less thanx < y
<=Less than or equal tox <= y
<=>combined comparision operatorx <=> y
===equality operator(1...5) === 3
.eql?equality operator, returns true one if both are of same type and equal valuesx.eql?y
equal?equality operator, returns true one if both are of same object IDx.equal?y

3. Bitwise Operators

Bitwise operators are used to perform bitwise operations on operands.

OperatorDescriptionUsage
&Bitwise AND(x > y) & (y > z)
|Bitwise OR(x > y) | (y > z)
^Bitwise XOR(x > y) ^ (y > z)
~Bitwise NOT(~x)
<<Bitwise Left Shiftx << y
>>Bitwise Right Shiftx >> y

4. Logical Operators

Logical operators are as shown below:

OperatorDescriptionUsage
&&Logical AND(x > y) && (y > z)
||Logical OR(x > y) || (y > z)
!Logical NOT(!x)
andLogical AND(x > y) and (y > z)
orLogical OR(x > y) or (y > z)
notLogical NOTnot(x && y)

5. Assignment Operators

Assignment operators are as shown below:

OperatorDescriptionUsage
=Assignx = 10;
+=Add and assignx=10; x+=30; #x becomes 40
-=Subtract and assignx=40; x-=10; #x becomes 30
*=Multiply and assignx=10; x*=40; #x becomes 400
/=Divide and assignx=100; x /= 10; #x becomes 10
%=Modulus and assignx=100; x%=10; #x becomes 0
**=exponential calculation and assignx **= y is equivalent to x = x**y

6. Ternary Operator

If the operator is applied on a three operands then it is called ternary. This is also known as conditional operator as a condition is followed by ? and true-expression which is followed by a : and false expression. This is oftenly used as a shortcut to replace if-else statement

OperatorDescriptionUsage
? :Conditional Expression(x > y) ? (code if x > y) : (code if x < y)

7. Range Operators

Below are the range operators:

OperatorDescriptionUsage
..Creates a range from start to end including end range1..10, creates a range from 1 to 10
...Creates a range from start to end excluding end range1...10, creates a range from 1 to 9

8. Special Operators

OperatorDescriptionUsage
defined?It is a special operator which is like a method call, returns a description string of the expression if defined else nildefined? variable or a method call
dot(.)Used to refer a method present in a modulemodulename.method
::Used to refer constants, instance methods and class methods defined within a class or module, to be accessed from anywhere outside the class or moduleModulename::const

Summary

TypeOperators
Arithmetic Operators+ , - , * , / , % , **
Comparision Operators== , != , > , >= , < , <= , <=>, .eql?, ===, equal?
Bitwise Operators& , ^ , | , ^ , ~ , << , >>
Logical Operators&& , || , ! , and , or, not
Assignment Operators= , += , -= , *= , /= , %= , **=
Ternary Operators? :
Range Operators.. , ...
Special Operatorsdefined?, . , ::