A Quick Guide To Ruby Operators
As we work with any other language and getting started with any other language we are so used to get through the various fundamentals of that particular language. One of these fundamentals
are the operators that are used in that language so that we can apply logic to our code and add certain functionality to our application and through this, we can make our application more towards the solution of the problem, so let as us check out how many types of operators we have in Ruby programing language. Many of them are listed below
Types Of Operators:
Now let us have a brief look at all the forms of operators
Arithmetic Operators -:
The arithmetic operators are used to perform arithmetic operations on the operands there are six types of arithmetic operators in ruby. These are as follows :
1. Subtraction (x-y):- Subtracts the first operand from the second
2. Addition (x+y):- This is used to add the value of two operands
3. Division (x/y):- This is used to divide two operands
4.Multiplication(x*y):-This is used to multiply two operands
5. Modulus (x%y):- This is used to take the modulus or the reminder of two operands
6. Power (x**y):- This is used to get the power of the first operand raised to second operand
example :
Logical Operators -:
To, perform a logical operation in our code we need to have a better understanding of the logical various types of logical operators and ruby language provides all of them to us, these operators are listed below:
1. Logical And (&&):- The logical &&(ANd ) is used to connect two logical statements. It is mainly used with the control flow statements such as "if ". In such cases, both
statements must be true.
2.Logical OR (||):- The logical ||(OR) is the same as the logical AND but it only demands on the statement to be true so that it can execute the code inside it.
3.Logical NOT(!):-The logical! (NOT ) returns the compliment of the given value.
Example:

Comparison Operators -:
The comparison operators are used to compare the value of two operands and returns the value is true or false. The following are the various types of comparison operators used in ruby.
1. equal to (= =):- This compares the value of both the operands and checks whether the values in both the operands are equal or not.
2. NOT equal to (!=):- This is used to check whether the given operands are not equal.
3. The comparator operator(<=>):- This will return 0 if both the operands are equal and -1 if the second operand is greater than the first and 1 if the first operand is greater than second
4. greater than(>)
5.less than (<)
6.greater than equal to (>=)
7. less than equal to (<=)
Assignment Operators -:
The assignment operator is used to assign values into the variables and this operator when combines with arithmetic operators turn out to very use full this is used as follows.
0.Normal Assignment Operator (=):- This is the basic assignment operator and is used to fill up the value into the variable
1. Assignment with Addition(+=):- This is used when we have to add the value in the same variable with an existing value
2.Assignment with Subtraction(-=):-This is used when we have to subtract the value in the same variable with an existing value
3.Assignment with Multiplication(*=):-This is used when we have to multiply the value in the same variable with an existing value
4.Assignment with Division(/=):-
5.Assignment with Modulus(%=):-
6.Assignment with Exponent(**=):-
Example :
Bitwise Operators -:
The bitwise operators are used to operate on bits there are various types of operators that can be used in ruby some of them are listed below.
- Bitwise AND (&) Takes two numbers as operands and does AND on every bit of two numbers. The result of AND is 1 only if both bits are 1.
- Bitwise OR (|) Takes two numbers as operands and does OR on every bit of two numbers. The result of OR is 1 any of the two bits is 1.
- Bitwise XOR (^) Takes two numbers as operands and does XOR on every bit of two numbers. The result of XOR is 1 if the two bits are different.
- Left Shift (<<) Takes two numbers, left shifts the bits of the first operand, the second operand decides the number of places to shift.
- Right Shift (>>) Takes two numbers, right shifts the bits of the first operand, the second operand decides the number of places to shift.
- Ones Complement (~) This operator takes a single number and used to perform a complement operation of 8-bit.
Ternary Operator (?:)-:
The ternary operator is used to check a condition before assigning the value to the variable and some time is used in place of an if-else block. Rest the concept would be clear from the following example :
Example:
Range Operators -:
The range operator is a special type of operator in ruby that is used to output the range of values between two endpoints that are defined by the programmer. There are two types of operators that are used to declare a range. Both have a very slight difference that we will discuss later in this section
1. range with two dot's(a..b):- The range defined by the double dot's ".." operator is used when we want to include all the operands into the value of range that are using.
2. Range with three-dots(a...b):- The range defined by the triple dot's "..." operator is used when we want to include the first but not the last operand in the range that we are defining.
Example:
defined? Operator -:
This operator is used to check whether the variable is defined or not and if the value is defined then it will return the type in which they are defined.
Dot"." and Double Colon"::" Operators -:
1. The Dot"." operator:-
This is used to access the methods of a class through the class name and also the value of the variable for the instance of that class. Examples are as follows:
2.The Double Colon"::" operators:-
The double-colon is used to access the value or the class, module, and methods inside the class or the module. Without using the instance of the class and this is the difference between using double-colon"::" and single dot "."
Ruby Splat Operator(||=) -:
This Operator checks whether the variable is defined or not and if it is not defined then it defines the variable and also places the value into the variable.
Example :
Difference between "= = " and " = = = " operator -:
The double-equal (= =) checks the value that is stored into the variable and if the value stored is equal in both the operands it gives true. On the other hand, if we use the triple-equal (= = =) operator this will
check the value stored into the variable as well as the type of the variable and return true only if both the type and value of both the operands are same.
Summary -:
By going through the above article you will be having enough knowledge to operate and use logic in your ruby code. So go ahead and try to practice the ruby code by your self.
Comments
Post a Comment