Signup/Sign In

Operators and Expressions in Ruby

In this lesson, we are going to look at the different operators that ruby contains and how to use them in the expressions.


Ruby: Addition Operator

+ symbol is used. It is both binary and unary operator. As the name suggests a Binary operator needs two operands/values on the either side of the operator to perform an operation and a unary operator needs just a single operand. Unary Plus serves no purpose, it is present just for the symmetry with unary minus.

For example : 1 + 2 //binary addition
value = +3 //unary plus

Ruby: Subtraction Operator

- symbol is used. You can use unary minus to reverse sign of a variable.

For Example : 23.2 - 2.2 //binary subtraction
value = -3 //unary minus

Ruby: Multiplication Operator

* symbol is used. Perfoms Multiplication on two numeric operands.

For Example : 122 * 3.14

Ruby: Division Operator

/ symbol is used. Returns result of devision of the first numeric operand by second operand.

For Example : 18 / 3

Arithmatic Operations in ruby


Ruby: Modulo Operator

Returns remainder after division.

For Example : 13 % 2 //returns 1

Modulo Operator in ruby


Ruby: Exponential Operator

If you want to raise x to the power of y (i.e) x ^ y.

It is done by x ** y

Exponential Operators in ruby

2 raised to the power 4 returns 16.


Ruby: Operator Precedence

Operators have some order of precedence which determines the order in which an expression will be evaluated.

Consider this example, 1 + 2 * 3

We might think that the 1 + 2 is performed and the result 3 will be multiplied by 3 and gives 9. But the multiplication, division and exponential operator have higher precedence than addition and subtraction operators. Therefore, 2 *3 is performed first and the result is added to 1 and gives 7 as an answer.

Operator Precedence in Ruby


However, we can modify the order of precedence by putting a subexpression in parentheses. In the expression 1 + 2 * 3, if 1 + 2 need to be performed first, put that expression in parentheses.

(1 + 2) * 3 this expression produces 9

Operator Precedence in Ruby


Ruby: Relational Operators

Relational operators are used for comparisons. They return Boolean values. We compare two values whether they are equal, not equal, less than, greater than, less than or equal to and greater than or equal to.

== sign is used. Used to check whether two numbers are equal or not.

1 == 1 returns true because 1 is equal to 1.

< - Less than

> - Greater than

<= - Less than or equal to

>= - Greater than or equal to

Less than operator checks whether a number is less than the another number, if yes it returns true else returns false. Less than or equal to operator checks whether a number is less than to another number and also checks whether a number is equal to another number, if any one of the condition is correct it returns true else returns false. Greater than and Greater than or equal to does the same and checks whether it is greater.

Relational Operators in Ruby

Another way to compare two values is using General comparison operator. It returns 0(zero), -1 or +1 depending on the operands. If both the values are equal it returns zero, if the first operand is less than the second operand it returns -1 and +1 if the first operand is greater than the second.

The General Comparison operator is <=>(< = >).

General Comparision Operator in Ruby

Relational operators can be used with strings also.

Comparision operator on Strings in Ruby

If it is same it returns true. You could see that it returns false for the operation 'Apple' == 'apple' This is because Ruby is case-sensitive and one of the word has Uppercase A while the other word has lowercase a. We can also use General comparison operator with strings.

General Comparision Operator on String in Ruby

When comparing 'car' and 'car' it returned 0 since both are equal. When comparing 'cab' with 'car' it returned -1 because the 3rd letter of the word 'b' in 'cab' is less than 'r' in 'car'.


Ruby: Logical Operators

Logical operators allow you to combine two or more relational expressions and returns Boolean value.

  • AND operator:

It returns true when all of the expressions are true and returns false if even one of the expression evaluates to false. You could use this operator using and or &&.

Logical And Operator in Ruby

It returned true for the expression salary == 10 && hours == 40 because we've initialized the value of the variables salary and hours as 10 and 40. It will return false for all other cases.

  • OR operator:

OR operator returns true when any one condition/expression is true and returns false only when all of them are false. You can use this operator using or (or) ||

Logical Or Operators in Ruby

You can see that even when only one expression is true OR operator returned true.

  • NOT operator:

NOT operator negates a relational expression. You can use not (or) ! for this operator.

Logical Not Operator in Ruby

In the first expression it returned false because the expression salary == 10 returns true and the not operator negates true and returns false. Likewise, the expression salary < 10 returns false and not operator negates and returns true.