Operators in Ruby
Ruby offers various types of operators to perform the conventional operations like assignment, conditional checks, addition, subtraction, etc.
Types of Operators
- Arithmetic operator
- Relational operator
- Logical operator
- Bitwise operator
- Assignment operator
- Range operator
- Dot and Colon operator
- Defined operator
- Ternary operator
Ruby Arithmetic Operator
Ruby provides the Arithmetic operator and it requires two operands to perform the operation. It performs the operations like addition, subtraction, multiplication, division, modulo.
Name |
Operator |
Description |
Addition |
+ |
It adds two numbers i.e. a + b |
subtraction |
- |
It subtracts two numbers i.e. a - b |
Multiplication |
* |
It multiples two numbers i.e. a * b |
Division |
/ |
It divides two numbers i.e. a / b |
Modulo (or) Modulus |
% |
It returns the remainder of the division operation between two numbers i.e. a / b |
Example: #1 - Arithmetic operator
Print the value of a and b with various Arithmetic operation.
Ruby Input Screen
x = 5
y = 3
puts("The value of (x + y) is " + (x + y).to_s)
puts("The value of (x - y) is " + (x - y).to_s)
puts("The value of (x * y) is " + (x * y).to_s)
puts("The value of (x / y) is " + (x / y).to_s)
puts("The value of (x % y) is " + (x % y).to_s)
Ruby Output Screen
The value of (x + y) is 8
The value of (x - y) is 2
The value of (x * y) is 15
The value of (x / y) is 1
The value of (x % y) is 2
Example: #2 - Arithmetic operator division
Divide a number with different values of data type int and float and print the result respectively.
Ruby Input Screen
i = 5
j = 2
k = 2.0
# By default, the data type of the result is int (i.e. 2)
puts("The value of (i / j) is " + (i / j).to_s)
# By default, the data type of the result is float (i.e. 2.5)
puts("The value of (i / k) is " + (i / k).to_s)
# Cast the divident of type float (i.e. j)
# So the result is 2.5
puts("The value of (i / j.to_f) is " + (i / j.to_f).to_s)
Ruby Output Screen
The value of (i / j) is 2
The value of (i / k) is 2.5
The value of (i / j.to_f) is 2.5
Thousands Lived without Love, but not without water. So SAVE WATER.
Ruby Relational Operator
Ruby provides the Relational operator to compare two operand values of same data type. It performs the operations like
Logical AND &&,
Logical OR ||
and Logical NOT !.
Name |
Operator |
Description |
Equal to |
== |
Compares two values and returns true only if both are equal. |
Not Equal to |
!= |
Compares two values and returns true only if both are not equal. |
Less than |
< |
Compares two values and returns true only if the left hand side operand is less than to the right hand side operand. |
Greater than |
> |
Compares two values and returns true only if the left hand side operand is greater than to the right hand side operand. |
Less than or equal to |
<= |
Compares two values and returns true only if the left hand side operand is less than or equal to the right hand side operand. |
Greater than or equal to |
>= |
Compares two values and returns true only if the left hand side operand is greater than or equal to the right hand side operand. |
Example: #3 - Relational Operator
Example of Relational like Equal to ==, Not Equal to !=,
Less than <, Greater than >, Less than or equal to <=
and Greater than or equal to >=
Ruby Input Screen
name = "John"
age = 24
# String Comparision
result = name == "John" # true
puts("Check the name is John : " + result.to_s)
# Numeric Comparision
result = age == 24 # true
puts("Check the person age is 24 : " + result.to_s)
result = age != 20 # true
puts("Check the person age is not equal to 20 : " + result.to_s)
result = age < 18 # false
puts("Check the person is minor (i.e. less than 18) : " + result.to_s)
result = age > 58 # false
puts("Check the person is senior citizen (i.e. greater than 58) : " + result.to_s)
result = age <= 18 # false
puts("Check the person age is less than or equal to 18 : " + result.to_s)
result = age >= 24 # true
puts("Check the person age is greater than or equal to 24 : " + result.to_s)
Ruby Output Screen
Check the name is John : true
Check the person age is 24 : true
Check the person age is not equal to 20 : true
Check the person is minor (i.e. less than 18) : false
Check the person is senior citizen (i.e. greater than 58) : false
Check the person age is less than or equal to 18 : false
Check the person age is greater than or equal to 24 : true
Thousands Lived without Love, but not without water. So SAVE WATER.
Ruby Logical Operator
Ruby provides the Logical operator and it requires two operands to perform the operation. It performs the operations like Logical AND &&, Logical OR || and Logical NOT !.
Name |
Operator |
Description |
Logical AND |
&& |
Compares two expressions and returns true only if both evaluate to true. |
Logical OR |
|| |
Compares two expressions and returns true either one evaluate to true. |
Logical NOT |
! |
Inverts the boolean value of an expression |
Example: #4 - Logical Operator
Example of Logical AND &&, Logical OR || and Logical NOT !
Ruby Input Screen
john = 32
peter = 19
# Logical AND operation
result1 = (john > 18) && (peter > 18);
result2 = (john < 18) && (peter < 18);
puts("Both are above 18 years (Logical AND) : " + result1.to_s);
puts("Both are below 18 years (Logical AND) : " + result2.to_s);
# Logical OR operation
result3 = (john > 18) || (peter > 18);
result4 = (john > 30) || (peter > 30);
result5 = (john > 50) || (peter > 50);
puts("Anyone is above 18 years (Logical OR) : " + result3.to_s);
puts("Anyone is above 30 years (Logical OR) : " + result4.to_s);
puts("Anyone is above 50 years (Logical OR) : " + result5.to_s);
# Logical NOT operation
result6 = !(john > 18);
puts("Check the john's age is below 18 years : " + result6.to_s);
Ruby Output Screen
Both are above 18 years (Logical AND) : true
Both are below 18 years (Logical AND) : false
Anyone is above 18 years (Logical OR) : true
Anyone is above 30 years (Logical OR) : true
Anyone is above 50 years (Logical OR) : false
Check the john's age is below 18 years : false
Thousands Lived without Love, but not without water. So SAVE WATER.
Ruby Bitwise Operator
Ruby provides the Bitwise operator and it requires two operands to perform the operation. It performs the operations like Signed Left Shift <<, Signed Right Shift >> and Unsigned Right Shift >>>.
Name |
Operator |
Description |
Left Shift |
<< |
Converts the numerical representation to the equivalent binary representation and shifts a 0 into the rightmost n position |
Signed Right Shift |
>> |
Converts the numerical representation to the equivalent binary representation and shifts 1 only if the number is negative otherwise 0 into the leftmost n position |
Unsigned Right Shift |
|
Converts the numerical representation to the equivalent binary representation and shifts a 0 into the leftmost n position.
In Ruby there is no operator available for Unsigned Right Shift but we can achieve in another way. |
Bitwise AND |
& |
Converts the numerical representation to the equivalent binary representation and perform the logical AND && for each bit against two operands |
Bitwise OR |
| |
Converts the numerical representation to the equivalent binary representation and perform the logical OR || for each bit against two operands |
Bitwise XOR |
^ |
Converts the numerical representation to the equivalent binary representation and perform the logical XOR for each bit against two operands |
Bitwise Complement |
~ |
Converts the numerical representation to the equivalent binary representation and perform the logical NOT ! for each bit. |
Example: #5 - Bitwise Shift Operator
Example of Bitwise operators - Left Shift <<, Signed Right Shift >> and Unsigned Right Shift >>>
Ruby Input Screen
x = -6
y = 6
result = 0
# Left Shift
# Remove the leftmost two binary digits
# and place the binary b'0' to the rightmost 2 position
# to make it 32 bit in length
# x => 11111111 11111111 11111111 11111010 => -6
# x << 2 => 11111111 11111111 11111111 11101000 => -24
# y => 00000000 00000000 00000000 00000110 => 6
# y << 2 => 00000000 00000000 00000000 00011000 => 24
result = x << 2;
puts("Left Shift (x << 2) is : " + result.to_s);
result = y << 2;
puts("Left Shift (y << 2) is : " + result.to_s);
# Signed Right Shift - Negative Number
# Remove the rightmost two binary digits
# and place the binary b'1' to the leftmost 2 position
# to make it 32 bit in length
# x => 11111111 11111111 11111111 11111010 => -6
# x >> 2 => 11111111 11111111 11111111 11111110 => -2
# Signed Right Shift - Positive Number
# Remove the rightmost two binary digits
# and place the binary b'0' to the leftmost 2 position
# to make it 32 bit in length
# y => 00000000 00000000 00000000 00000110 => 6
# y >> 2 => 00000000 00000000 00000000 00000001 => 1
result = x >> 2;
puts("Signed Right Shift (x >> 2) is : " + result.to_s);
result = y >> 2;
puts("Signed Right Shift (y >> 2) is : " + result.to_s);
# Unsigned Right Shift
# Remove the rightmost two binary digits
# and place the binary b'0' to the leftmost 2 position
# to make it 32 bit in length
# x => 11111111 11111111 11111111 11111010 => -6
# x >>> 2 => 00111111 11111111 11111111 11111110 => 1073741822
# y => 00000000 00000000 00000000 00000110 => 6
# y >>> 2 => 00000000 00000000 00000000 00000001 => 1
mask = (1 << (32 - 2)) - 1
result = (x >> 2) & mask
puts("Unsigned Right Shift (x >>> 2) is : " + result.to_s)
mask = (1 << (32 - 2)) - 1
result = (y >> 2) & mask
puts("Unsigned Right Shift (y >>> 2) is : " + result.to_s)
Ruby Output Screen
Left Shift (x << 2) is : -24
Left Shift (y << 2) is : 24
Signed Right Shift (x >> 2) is : -2
Signed Right Shift (y >> 2) is : 1
Unsigned Right Shift (x >>> 2) is : 1073741822
Unsigned Right Shift (y >>> 2) is : 1
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #6 - Bitwise Operator
Example of Bitwise operators - Bitwise AND &, Bitwise OR |, Bitwise XOR ^ and Bitwise Complement ~.
Ruby Input Screen
x = 6
y = 3
result = 0
# Bitwise AND
# x => 00000000 00000000 00000000 00000110 => 6
# y => 00000000 00000000 00000000 00000011 => 3
# x & y => 00000000 00000000 00000000 00000010 => 2
result = x & y;
puts("Bitwise AND (x & y) is : " + result.to_s);
# Bitwise OR
# x => 00000000 00000000 00000000 00000110 => 6
# y => 00000000 00000000 00000000 00000011 => 3
# x | y => 00000000 00000000 00000000 00000111 => 7
result = x | y;
puts("Bitwise OR (x | y) is : " + result.to_s);
# Bitwise XOR
# x => 00000000 00000000 00000000 00000110 => 6
# y => 00000000 00000000 00000000 00000011 => 3
# x ^ y => 00000000 00000000 00000000 00000101 => 5
result = x ^ y;
puts("Bitwise XOR (x ^ y) is : " + result.to_s);
# Bitwise Complement
# x => 00000000 00000000 00000000 00000110 => 6
# ~x => 11111111 11111111 11111111 11111001 => -7
result = ~x;
puts("Bitwise Complement ~x is : " + result.to_s);
Ruby Output Screen
Bitwise AND (x & y) is : 2
Bitwise OR (x | y) is : 7
Bitwise XOR (x ^ y) is : 5
Bitwise Complement ~x is : -7
Ruby Assignment Operator
Ruby provides the Assignment operator and it requires two operands to perform the operation. It performs the operations like addition, subtraction, multiplication, division, modulo.
Name |
Operator |
Description |
Simple Assignment |
= |
Assigns a value to a variable. |
Increment Assignment |
+= |
Adds a value and the variable and assigns the result to that variable. |
Decrement Assignment |
-= |
Subtracts a value and the variable and assigns the result to that variable. |
Multiplication Assignment |
*= |
Multiplies a value and the variable and assigns the result to that variable. |
Division Assignment |
/= |
Divides a value and the variable and assigns the result to that variable. |
Modulo Assignment / Modulus assignment |
%= |
Modulo a value and the variable and assigns the result to that variable. |
Bitwise Left shift assignment |
<<= |
Performs a Bitwise Left shift operation between a variable and a value, finally assigns the result to that variable. |
Bitwise Singed Right shift assignment |
>>= |
Performs a Bitwise Singed Right shift operation between a variable and a value, finally assigns the result to that variable. |
Bitwise AND assignment |
&= |
Performs a Bitwise AND operation between a variable and a value, finally assigns the result to that variable. |
Bitwise OR assignment |
|= |
Performs a Bitwise OR operation between a variable and a value, finally assigns the result to that variable. |
Bitwise XOR assignment |
^= |
Performs a Bitwise XOR operation between a variable and a value, finally assigns the result to that variable. |
Example: #7 - Arithmetic Assignment operator
Print the resultant value of x, y with various Arithmetic Assignment operation.
Ruby Input Screen
x = 6
y = 1
y += x; # 6 + 1 = 7
puts("The value of y => (y += x) is " + y.to_s);
y = 10;
y -= x; # 10 - 6 = 4
puts("The value of y => (y -= x) is " + y.to_s);
y = 2;
y *= x; # 6 * 2 = 12
puts("The value of y => (y *= x) is " + y.to_s);
y = 18;
y /= x; # 18 / 6 = 3
puts("The value of y => (y /= x) is " + y.to_s);
y = 20;
y %= x; # 20 % 6 = 2
puts("The value of y => (y %= x) is " + y.to_s);
Ruby Output Screen
The value of y => (y += x) is 7
The value of y => (y -= x) is 4
The value of y => (y *= x) is 12
The value of y => (y /= x) is 3
The value of y => (y %= x) is 2
Thousands Lived without Love, but not without water. So SAVE WATER.
Example: #8 - Bitwise Assignment operator
Print the resultant value of x, y with various Bitwise Assignment operation.
Ruby Input Screen
x = 2
y = 6
y <<= x; # 6 << 2 = 24
puts("The value of y => (y <<= x) is " + y.to_s)
x = 2
y = 6
y >>= x # 6 >> 2 = 1
puts("The value of y => (y >>= x) is " + y.to_s)
x = 3
y = 6
y &= x # 6 & 3 = 2
puts("The value of y => (y & x) is " + y.to_s)
x = 3
y = 6
y |= x # 6 | 3 = 7
puts("The value of y => (y | x) is " + y.to_s)
x = 3
y = 6
y ^= x # 6 ^ 3 = 5
puts("The value of y => (y ^ x) is " + y.to_s)
Ruby Output Screen
The value of y (y <<= x) is 24
The value of y (y >>= x) is 1
The value of y (y &= x) is 2
The value of y (y |= x) is 7
The value of y (y ^= x) is 5
Thousands Lived without Love, but not without water. So SAVE WATER.
Ruby Range Operator
Ruby provides the Range operator and it requires two operands to perform the operation. It will generate the sequence of numbers within a range of two numeric operands.
Name |
Operator |
Description |
Double Dot |
.. |
Double dot generate a sequence of numbers from start to end, which includes the starting and ending numer i.e. 6..9 => 6, 7, 8, 9 |
Triple Dot |
... |
Triple dot generate a sequence of numbers from start to end, which includes the starting number and excludes the ending numer i.e. 1..3 => 6, 7, 8 |
Example: #9 - Range Operator
Example of Range Operator to generate a sequence of numbers using Double dot .. and Triple dot ...
Ruby Input Screen
print("Generate a sequence for 6..9 => ")
seq = 6..9
for i in seq do
print(i.to_s + " ")
end
print("\nGenerate a sequence for 6...9 => ")
seq = 6...9
for i in seq do
print(i.to_s + " ")
end
Ruby Output Screen
Generate a sequence for 6..9 => 6 7 8 9
Generate a sequence for 6...9 => 6 7 8
Thousands Lived without Love, but not without water. So SAVE WATER.
Ruby Dot and Colon Operator
Ruby provides the Range operator and it requires two operands to perform the operation. It will generate the sequence of numbers within a range of two numeric operands.
Name |
Operator |
Description |
Double Dot |
.. |
Double dot generate a sequence of numbers from start to end, which includes the starting and ending numer i.e. 6..9 => 6, 7, 8, 9 |
Triple Dot |
... |
Triple dot generate a sequence of numbers from start to end, which includes the starting number and excludes the ending number i.e. 1..3 => 6, 7, 8 |
Example: #10 - Range Operator
Example of Range Operator to generate a sequence of numbers using Double dot .. and Triple dot ...
Ruby Input Screen
print("Generate a sequence for 6..9 => ")
seq = 6..9
for i in seq do
print(i.to_s + " ")
end
print("\nGenerate a sequence for 6...9 => ")
seq = 6...9
for i in seq do
print(i.to_s + " ")
end
Ruby Output Screen
Generate a sequence for 6..9 => 6 7 8 9
Generate a sequence for 6...9 => 6 7 8
BBMINFO