Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Ruby Operators
In this tutorial, you will learn about Ruby operators and their types with the help of examples.
Operators are special symbols that perform operations on variables and values. For example,
puts 5 + 6 # Output: 11
Here, + is an operator that adds two numbers: 5 and 6.
Types of Ruby Operators
Here's a list of different types of Ruby operators that we will learn in this tutorial:
- Arithmetic Operators
- Assignment Operators
- Comparison Operators
- Logical Operators
- Bitwise Operators
- Ternary Operator
Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc. For example,
mult = 2 * 5
puts mult # Output: 10
Here, * is an arithmetic operator that multiplies two values 2 and 5.
Some commonly used arithmetic operators are:
| Operator | Operation | Example | Result |
|---|---|---|---|
+ |
Addition | 5 + 2 |
7 |
- |
Subtraction | 4 - 2 |
2 |
* |
Multiplication | 2 * 3 |
6 |
/ |
Division | 4 / 2 |
2 |
% |
Modulo | 5 % 2 |
1 |
** |
Exponentiation | 4 ** 2 |
16 |
Example 1: Arithmetic Operators
Let's explore an example to see how various arithmetic operators work.
a = 7
b = 2
# Addition
puts a + b # Output: 9
# Subtraction
puts a - b # Output: 5
# Multiplication
puts a * b # Output: 14
# Division
# Ruby performs integer division when both values are integers.
# So,it returns only the whole number part.
puts a / b # Output: 3
# Modulo
puts a % b # Output: 1
# Exponentiation
puts a ** b # Output: 49
Assignment Operators
The assignment operator = is used to assign values to a variable. For example,
# Assigns 5 to num
num = 5
puts num
Output
5
Here, we've used the = operator to assign the value 5 to the variable num.
Some commonly used assignment operators in Ruby are:
| Operator | Name | Example |
|---|---|---|
= |
Assignment Operator | a = 7(Value of a is 7) |
+= |
Addition Assignment | a += 5(Same as a = a + 5) |
-= |
Subtraction Assignment | a -= 2(Same as a = a - 2) |
*= |
Multiplication Assignment | a *= 3(Same as a = a * 3) |
/= |
Division Assignment | a /= 2 (Same as a = a / 2) |
%= |
Remainder Assignment | a %= 4(Same as a = a % 4) |
**= |
Exponentiation Assignment | a **= 2(Same as a = a ** 2) |
Example 2: Assignment Operators
# Using Assignment Operator
num = 7
# Using Addition Assignment
num += 5
puts "After += 5: #{num}"
# Using Subtraction Assignment
num -= 2
puts "After -= 2: #{num}"
# Using Multiplication Assignment
num *= 3
puts "After *= 3: #{num}"
# Using Division Assignment
num /= 2
puts "After /= 2: #{num}"
# Using Remainder Assignment
num %= 4
puts "After %= 4: #{num}"
# Using Exponentiation Assignment
num **= 2
puts "After **= 2: #{num}"
Output
After += 5: 12 After -= 2: 10 After *= 3: 30 After /= 2: 15 After %= 4: 3 After **= 2: 9
Here, after each assignment, the value of num changes, and the new value is used in the next step.
Comparison Operators
The comparison operators compare two values and return a boolean value (true or false) based on the comparison result. For example,
a = 2
b = 5
# Checks if 'a' is less than 'b'
puts a < b
# Output: true
Here, we've used the less than (<) comparison operator to compare the values of a and b. Since 2 is less than 5, the expression a < b evaluates to true.
Some commonly used comparison operators are:
| Operator | Name | Example | Result |
|---|---|---|---|
== |
Equal to | 3 == 5 |
false |
!= |
Not equal to | 3 != 4 |
true |
> |
Greater than | 4 > 4 |
false |
< |
Less than | 3 < 3 |
false |
>= |
Greater than or equal to | 4 >= 4 |
true |
<= |
Less than or equal to | 3 <= 3 |
true |
<=> |
Combined comparison (returns -1, 0, or 1) | 5 <=> 10 |
-1 |
Example 3: Comparison Operators
a = 5
b = 2
# Equal to operator
puts "#{a} == #{b} is #{a == b}"
# Not equal to operator
puts "#{a} != #{b} is #{a != b}"
# Greater than operator
puts "#{a} > #{b} is #{a > b}"
# Less than operator
puts "#{a} < #{b} is #{a < b}"
# Greater than or equal to operator
puts "#{a} >= #{b} is #{a >= b}"
# Less than or equal to operator
puts "#{a} <= #{b} is #{a <= b}"
# Combined comparison operator
puts "#{a} <=> #{b} is #{a <=> b}"
Output
5 == 2 is false 5 != 2 is true 5 > 2 is true 5 < 2 is false 5 >= 2 is true 5 <= 2 is false 5 <=> 2 is 1
Logical Operators
Logical operators are used to check whether an expression is true or false. They are used in decision-making. For example,
a = 3
b = 6
puts (a > 2) && (b >= 6) # Output: true
Here, we've used the logical AND operator (&&). Since both a > 2 and b >= 6 are true, the result is true.
The commonly used logical operators are:
| Operator | Syntax | Description |
|---|---|---|
&& (Logical AND) |
a && b |
Returns true only if both a and b are true. |
|| (Logical OR) |
a || b |
Returns true if either a or b is true. |
! (Logical NOT) |
!a |
Returns false if a is true and vice versa. |
Example 4: Logical Operators
# Logical AND
has_password = true
has_username = true
can_login = has_password && has_username
puts "Can login: #{can_login}"
# Logical OR
is_weekday = false
is_holiday = true
can_relax = is_weekday || is_holiday
puts "Can relax: #{can_relax}"
# Logical NOT
is_busy = false
is_free = !is_busy
puts "Is free: #{is_free}"
Output
Can login: true Can relax: true Is free: true
Here,
| Expression | Result | Reason |
|---|---|---|
has_password && has_username |
true |
Both has_password and has_username are true. |
is_weekday || is_holiday |
true |
is_weekday is false, but since is_holiday is true, the OR operator returns true. |
!is_busy |
true |
is_busy is false, and the NOT operator inverts the false to true. |
Bitwise Operators
Bitwise operators act on operands as if they were strings of binary digits. They operate bit by bit, hence the name.
In the table below:
Let a = 10 (0000 1010 in binary) and b = 4 (0000 0100 in binary)
| Operator | Syntax | Description |
|---|---|---|
& (Bitwise AND) |
a & b |
0 (0000 0000)Copies bits set in both a and b |
| (Bitwise OR) |
a | b |
14 (0000 1110)Copies bits set in either a or b |
~ (Bitwise NOT) |
~a |
-11 (1111 0101)Flips all bits in a |
^ (Bitwise XOR) |
a ^ b |
14 (0000 1110)Copies bits set in one but not both of a and b |
>> (Bitwise right shift) |
a >> 2 |
2 (0000 0010)Shifts a right by 2 bits |
<< (Bitwise left shift) |
a << 2 |
40 (0010 1000)Shifts a left by 2 bits |
Note: Binary values are shown in 8-bit format for clarity. Ruby uses arbitrary-precision integers, so actual storage may differ.
Ternary Operator
The ternary operator in Ruby consists of three parts:
- A condition.
- An expression to execute if the condition is
true. - Another expression to execute if the condition is
false.
The syntax of the ternary operator is:
condition ? Expression1 : Expression2
Let's see a simple example of the ternary operator.
age = 18
can_vote = age >= 18 ? "Yes, you can vote." : "No, you cannot vote."
puts can_vote
Output
Yes, you can vote.
Here,
- Condition:
age >= 18checks ifageis 18 or greater. - Expression1:
"Yes, you can vote."is returned if the condition istrue. - Expression2:
"No, you cannot vote."is returned if the condition isfalse.
Special Operators
In Ruby, range operators are used to create a sequence of values from a start point to an endpoint. There are two types of range operators:
| Operator | Description | Example |
|---|---|---|
Inclusive Range (..) |
Includes the end value. | 1..5 → 1, 2, 3, 4, 5 |
Exclusive Range (...) |
Excludes the end value. | 1...5 → 1, 2, 3, 4 |
The dot (.) operator is used to call methods on objects. For example,
greeting = "hello"
puts greeting.upcase # Output: HELLO
Here, .upcase calls the upcase method on the string "hello".
The double colon (::) operator is used to access constants, classes, or modules defined inside another module or class. For example,
puts Math::PI # Output: 3.141592653589793
The defined? operator checks if a variable, method, or constant exists. It returns a string if it's defined, or nil if not. For example,
x = 10
puts defined? x # Output: local-variable
puts defined? puts # Output: method
puts defined? y # Output: nil
In Ruby, if defined? returns nil, puts prints nothing, because there's nothing to print.