Ruby Operators
Operators is an special symbols, which are used to compare, assignment, and perform logical and mathematical operation. That is a simple way to describe the role of an operator. But that is a basic level, operator is much powerful in programming. That are used basic and special purpose.
Generally operator are involved of many place in our code, Such as when, define a similar type of multiple variable then there are use comma operator to separating the variable. Dot operator are used to method of class and there are in similar way many operator are used to perform some specific task.
In this post are given examples which are commonly used to mathematical and logical operation.
Arithmetic Operator
Arithmetic Operator are work in two operands, that is also know as binary operators. We can thinking about this type of operators. This operator are works two operands such as left and right. And operator are exist in between of them. operator is decide to which operation are performed to those operand. In given table mention of all arithmetic operators.
Operator  Name 

+  Addition(+) 
  Subtract () 
*  Multiplication (*) 
/  Divide 
%  Remainder operator 
**  Exponent (power) 
Basically arithmetic operator are used to perform 6 type of operation. compiler are know about what is the role of symbol that are applied to operands (variable value or constant literals).
a = 10
b = 3
puts " a : #{a} , b : #{b}"
puts " (#{a} + #{b}) = #{a+b} "
puts " (#{a}  #{b}) = #{ab} "
puts " (#{a} * #{b}) = #{a*b} "
puts " (#{a} / #{b}) = #{a/b} " #divide two integer
puts " (#{a} % #{b}) = #{a%b} "
puts " (#{a} ** #{b}) = #{a**b} "
Output
a : 10 , b : 3
(10 + 3) = 13
(10  3) = 7
(10 * 3) = 30
(10 / 3) = 3
(10 % 3) = 1
(10 ** 3) = 1000
In Ruby Programming, (+) addition operator are used to adding the numeric value. () operator are used to Subtract two values, (*) operator are used to multiplication of two values. and similarly (/) operator are divide the two values and (%) are used to get remainder of two values. Modulus % operator are work only integer values.
Logical Operator
Logical operator are work in boolean values. && operator are used to check expression condition when both left and right the expression are produced boolean true value. Then that are produce an true boolean result. Otherwise produce false. In given list are mentioned logical operators.
Operator  Name 

&&  Logical AND operator. 
  Logical OR Operator 
!  Logical NOT Operator 
Example of logical AND operator.
result = true && true
puts result #true
result = false && false
puts result #false
result = true && false
puts result #false
result = false && true
puts result #false
Output
true
false
false
false
Logical Or () is an binary operator, That is work on boolean values. This operator are produce true value when any one expression are produced true result (left and right operand result).
result = true  true
puts result #true
result = false  false
puts result #false
result = true  false
puts result #true
result = false  true
puts result #trues
Output
true
false
true
true
Logical Not (!) is an unary operator that are work on boolean value. when expression is produce true value that is modified this result to false. and when expression is produce false boolean value then that is change this result to true boolean value.
trueStatus = true
falseStatus = !trueStatus #use logical not
puts "trueStatus : #{trueStatus}"
puts "falseStatus : #{falseStatus}"
Output
trueStatus : true
falseStatus : false
We can also use this operator to check logical condition result. The main role of Logical Not operator is inverts the boolean result. And this is prefix operator.
if(!(5 > 15))
# (5>15) = false => !false = true
print("Five is not greater than 15")
end
Output
Five is not greater than 15
Bitwise Operators
Bitwise operator is based on binary value and that are work on two operands. Normally all operators are produce boolean values but that is work on a binary bit level modification. In given table mentioned all Bitwise operator.
Operator  Name 

&  Binary AND operator. 
  Binary OR Operator 
^  Binary XOR Operator 
~  Binary Ones Complement Operator 
<<  Binary Left Shift Operator 
>>  Binary Right Shift Operator 
Binary AND operator
=begin
Example Binary And Operator
/*
* Binary of 5 = 0 1 0 1
* Binary of 4 = 0 1 0 0
* &
*
* Result 4 = 0 1 0 0
*/
=end
a = 5 & 4
print(a) #4
Output
4
Binary Or operator
=begin
Example Binary OR Operator
/*
* Binary of 3 = 0 0 1 1
* Binary of 4 = 0 1 0 0
* 
*
* Result 7 = 0 1 1 1
*/
=end
a = 3  4
print(a) #7
Output
7
Binary XOR operator
=begin
Example Binary XOR Operator
/*
* Binary of 5 = 0 1 0 1
* Binary of 4 = 0 1 0 0
^
*
* Result 1 = 0 0 0 1
When 0 1 = 1
When 0 0 = 0
When 1 1 = 0
*/
=end
a = 5 ^ 4
print(a) #1
Output
1
Binary Ones Complement Operator
data1=15
data2 = 6
=begin
/*Bitwise 1s Complement Operator
Example 1:
15 = 1 1 1 1 (binary of 15)
add 1 (binary of 1)
+

Result= 1 1 1 1 (binary of 16)

output=  Result
output=  16
Example 2: first remove that ( sign)
6 = 0 1 1 0 (binary of 6)
minus 1 (binary of 1)


Result= 0 1 0 1 (binary of 5)
Result= 5
//add remove sign
Result =5

//multiply by ( sign)
output=  Result
output=  (5)
output= 5
*/
=end
puts ~data1
puts ~data2
Output
16
5
Binary Left Shift Operator
data1 = 15
data2 = 2
=begin
/*Bitwise <<
Example 1:
15 = 0 0 1 1 1 1 (binary of 15)
left shift 2 = 1 1 1 1 0 0

60 = 1 1 1 1 0 0

Or
Formula = data * (2^ left shift)
example 1 = 15 *(2^2) 2 bit shift
= 15*4
= 60
example 2 = 2 *(2^5)
= 2 *(32)
= 64
*/
=end
puts 15 << 2 #60
puts 2 << 5 #64
Output
60
64
Binary Right Shift Operator
data1=15
data2=2
=begin
/*Bitwise >>
Example 1:
15 = 1 1 1 1 (binary of 15)
right shift 2 = 0 0 1 1

3 = 0 0 1 1

*/
=end
print(15>>2) #3
Output
3
Relational Operators
Relational operator are work on two operands, and normally that are used to compare operands value. This operator are produce boolean result. And which are normally used in conditional statement to check expression.
Operator  Name 

>  greater than 
<  less than 
==  equal to equal to 
>=  greater than equal to 
<=  less than equal to 
!=  Not equal to 
a = 5
b = 7
puts " a : #{a} , b : #{b}"
puts " (#{a} > #{b}) = #{a>b} "
puts " (#{a} < #{b}) = #{a= #{b}) = #{a>=b} "
puts " (#{a} <= #{b}) = #{a<=b} "
puts " (#{a} != #{b}) = #{a!=b} "
a : 5 , b : 7
(5 > 7) = false
(5 < 7) = true
(5 == 7) = false
(5 >= 7) = false
(5 <= 7) = true
(5 != 7) = true
Assignment Operators
Assignment operator are used to assign a value of right hand side to left hand variables. In given table are view short form of assignment operator. Which are perform some operation of two operand and result are store in the left side variable.
Operator  Meaning 

*=  Multiply and assign 
+=  Add and assign 
/=  Divide and assign 
=  Subtract and assign 
%=  calculate remainder and assign 
=  Equal 
Basically normal (=) assignment operator are used to set value of variable. This are assign value according to data type of variable. But short form we are combine this operator and arithmetic operator and perform two type of operation. First are performed arithmetic operation and resultant are store by assignment operator.
Ternary Conditional Operator
This operators is similar to C Ternary operator. There is divided into three section, first one are expression. If result of first expression result is true then work on second section expression. And if first expression is false then it will executing on third section expression.
//section 1 section 2 section 3
expression/condition ? Pass : Fail
a = 10
result = (a%2==0) ? "Even number" : "Odd Number"
puts result #Even number
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment