In this section, we will learn what the Operators are and why we need them in Ruby.
What Is an Operator in Ruby? Symbol in Ruby
Symbols like +, -, >, < etc. are known as operators. They allow us to run different sorts of operation in Ruby.
For example, if we want to multiply two values together, we can use the multiplication operator which has this * symbol. Or if we want to compare two values and see which one is greater than the other we can use the > operator which is known as the Greater Than operator.
Ruby Operands
An operator operates on values! For example, the multiplication operator takes two values and multiply them together. These values are known as operands.
Example: invoking Ruby operands
40 > 10
Here the operator is the Greater Than operator. It has two values 40 and 10 which are known as operands or more specifically the left hand operand and the right hand operand.
Ruby LValues and RValues
When working with assignment operators like `=` the operand that appears on the left side of the assignment operator is known as LValue (which stands for left value) and the value that appears on the right side of the assignment operators is called RValue (which stands for right value).
The important note to remember about LValues and RValues is that Lvalues must always be a variable that is pointing to a memory space! Basically, the lvalue should be backed by a memory space. This is because lvalues are the takers and they take any value that appears on the right side of an assignment operator. So if they are not backed by a memory space, then how they can take and hold the values they receive?!
On the other hand, RValues can be any type of values! Basically, they don’t need to be backed by a memory space! This means we can set a variable, a raw value, the result of calling a method etc. as the RValue.
Example: using lvalues and rvlaues in Ruby
val1 = "One" val2 = val1 val3 = 3+4 puts val1, val2, val3
Output:
One One 7
Note that the entire Lvalues in the example above are variables and they are backed by a memory space.
But on the right side of the assignment operator, we’ve passed any type of value or expression we wanted.
List of Ruby Operators
In Ruby, there are multiple different types of operators for different purposes. In later sections, we will dive deep into each of these operators, but for now, here’s an introductory list of these operators:
Ruby Arithmetic Operators
The Arithmetic operators are used to run mathematical type of operations in Ruby. For example, multiplication, addition, subtraction etc. are considered as arithmetic operators.
Operator |
Symbol |
Description |
Addition Operator |
+ |
Using this operator, we can add two values together. |
Subtraction Operator |
– |
Using this operator, we can subtract two values from each other. |
Multiplication Operator |
* |
Using this operator, we can multiply two values to each other. |
Division Operator |
/ |
Using this operator, we can subtract two values from each other. |
Exponentiation Operator |
** |
This operator is used to raise the value of the left operand to the power of the right operand and then return the result. |
Modulus Operator |
% |
Using this operator, we can run modulus operation on operands. |
Example: using arithmetic operators in Ruby
val1 = "Jack"+" Bauer" val2 = 3*4 puts val1, val2
Output:
Jack Bauer 12
Ruby Comparison Operators
These operators are used for comparing two or move values to see if they are equal or one is greater than the other.
Operator |
Symbol |
Description |
Equal |
== |
Using this operator we can check and see if two values are equal or not. |
Negate Equal |
!= |
Using this operator, we can check and see if two values are NOT equal. |
Greater than |
> |
Using this operator, we can check to see if the left operand is greater than the right operand. |
Less than |
< |
Using this operator, we can check to see if the right operand is greater than the left operand. |
Greater Than or equal to |
>= |
Using this operator, we can see if the left operand is either greater or equal compared to the right operand. |
Less than or equal to |
<= |
Using this operator, we can see if the right operand is either greater or equal compared to the left operand. |
The Spaceship Operator |
<=> |
Using this operator, we can declare whether a value is less than, greater than, or equal to another value |
Example: using comparison operators in Ruby
val1 = 50 val2 = 100 puts val1 > val2 puts val1 < val2
Output:
false true
Ruby Logical Operators
These operators are used to combine the results of comparing two or more comparison operators or even negate their results.
Operator |
Description |
and |
Using this operator, we can combine the result of two expressions and decide whether the final result should be true or false. |
or |
Using this operator, we can check two expressions and, based on the results of those expressions, we can decide if the final value should be true or false. |
not |
Using this operator, we can negate the result of an expression that results either true or false |
&& |
This operator acts the same as the `and` operator and so it will combine the result of two expressions and decide whether the final result should be true or false. |
|| |
This operator acts the same as the `or` operator. |
! |
This operator acts the same as the `not` operator. |
Example: using logical operators in Ruby
if 10 > 5 and 15 < 100 print ("The two expressions resulted true!") end
Output:
The two expressions resulted true!
Ruby Assignment Operators
We use the assignment operators to assign a value to a variable (making a variable to point to a value in a memory).
Operator |
Symbol |
Assignment operator |
= |
Addition and Assignment operator |
+= |
Subtraction and Assignment operator |
-= |
Multiplication and Assignment operator |
*= |
Division and Assignment operator |
/= |
Exponentiation and Assignment Operator |
**= |
Modulus and Assignment Operator |
%= |