Ruby Operators

Performing operations and calculations in Ruby

⚙️ What are Ruby Operators?

Operators are special symbols that perform operations on variables and values. Ruby supports arithmetic, comparison, logical, assignment, and many other operators to manipulate data and control program flow effectively.


# Basic operators
sum = 10 + 5        # Addition
product = 10 * 5    # Multiplication
is_equal = 10 == 5  # Comparison

puts sum            # Output: 15
puts is_equal       # Output: false
                                    

Types of Operators

Arithmetic

Mathematical calculations

10 + 5  # 15
10 - 5  # 5
⚖️

Comparison

Compare values

10 == 5  # false
10 > 5   # true
🔗

Logical

Boolean operations

true && false  # false
true || false  # true
📝

Assignment

Assign values

x = 10
x += 5  # x = 15

🔹 Arithmetic Operators

Arithmetic operators perform mathematical calculations like addition, subtraction, multiplication, and division on numeric values.

# Arithmetic operators
a = 20
b = 5

puts a + b    # Addition: 25
puts a - b    # Subtraction: 15
puts a * b    # Multiplication: 100
puts a / b    # Division: 4
puts a % b    # Modulus (remainder): 0
puts a ** b   # Exponentiation: 3200000

Output:

25
15
100
4
0
3200000

🔹 Comparison Operators

Comparison operators compare two values and return true or false. They're essential for conditional statements and decision-making.

# Comparison operators
x = 10
y = 5

puts x == y    # Equal to: false
puts x != y    # Not equal to: true
puts x > y     # Greater than: true
puts x < y     # Less than: false
puts x >= y    # Greater than or equal: true
puts x <= y    # Less than or equal: false

Output:

false
true
true
false
true
false

🔹 Logical Operators

Logical operators combine multiple conditions and return boolean results. They're used to create complex conditional expressions.

# Logical operators
a = true
b = false

puts a && b    # AND: false (both must be true)
puts a || b    # OR: true (at least one must be true)
puts !a        # NOT: false (inverts the value)

# Practical example
age = 25
has_license = true
puts age >= 18 && has_license  # Output: true

Output:

false
true
false
true

🔹 Assignment Operators

Assignment operators assign values to variables. Compound assignment operators combine arithmetic operations with assignment for shorter code.

# Assignment operators
x = 10        # Simple assignment
puts x        # Output: 10

x += 5        # x = x + 5
puts x        # Output: 15

x -= 3        # x = x - 3
puts x        # Output: 12

x *= 2        # x = x * 2
puts x        # Output: 24

x /= 4        # x = x / 4
puts x        # Output: 6

Output:

10
15
12
24
6

🔹 Range Operators

Range operators create sequences of values. Use .. for inclusive ranges and ... for exclusive ranges (excludes the last value).

# Range operators
inclusive = (1..5)      # Includes 5
exclusive = (1...5)     # Excludes 5

puts inclusive.to_a.inspect   # Output: [1, 2, 3, 4, 5]
puts exclusive.to_a.inspect   # Output: [1, 2, 3, 4]

# Using ranges in loops
(1..3).each do |i|
  puts i
end
# Output: 1, 2, 3

Output:

[1, 2, 3, 4, 5]
[1, 2, 3, 4]
1
2
3

🔹 Ternary Operator

The ternary operator is a shorthand for if-else statements. It evaluates a condition and returns one of two values.

# Ternary operator: condition ? true_value : false_value
age = 20
status = age >= 18 ? "Adult" : "Minor"
puts status  # Output: Adult

# Equivalent if-else
if age >= 18
  status = "Adult"
else
  status = "Minor"
end

# Another example
score = 85
result = score >= 60 ? "Pass" : "Fail"
puts result  # Output: Pass

Output:

Adult
Pass

🔹 String Operators

Ruby provides operators for string manipulation, including concatenation and repetition for working with text data.

# String concatenation
first_name = "John"
last_name = "Doe"
full_name = first_name + " " + last_name
puts full_name  # Output: John Doe

# String repetition
laugh = "Ha" * 3
puts laugh      # Output: HaHaHa

# String append
greeting = "Hello"
greeting << " World"
puts greeting   # Output: Hello World

Output:

John Doe
HaHaHa
Hello World

🔹 Operator Precedence

Operators have different priorities. Operations with higher precedence are performed first. Use parentheses to control evaluation order.

# Without parentheses
result1 = 10 + 5 * 2
puts result1  # Output: 20 (multiplication first)

# With parentheses
result2 = (10 + 5) * 2
puts result2  # Output: 30 (addition first)

# Complex expression
result3 = 10 + 5 * 2 - 3
puts result3  # Output: 17

# With parentheses for clarity
result4 = 10 + (5 * 2) - 3
puts result4  # Output: 17

Output:

20
30
17
17

💡 Operator Quick Reference:

  • Arithmetic: +, -, *, /, %, ** (exponent)
  • Comparison: ==, !=, >, <, >=, <=
  • Logical: && (and), || (or), ! (not)
  • Assignment: =, +=, -=, *=, /=
  • Range: .. (inclusive), ... (exclusive)
  • Ternary: condition ? true_value : false_value

🧠 Test Your Knowledge

What is the result of 10 % 3 in Ruby?