Operators
In Ruby, you have 9 main categories of operators:
- Arithmetic Operators
- Comparison Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Ternary Operator
- Range Operators
- Membership Operators
- Defined Operator
Arithmetic Operators
Arithmetic operators are used for performing mathematical operations on numeric values.
# Addition
result_addition = 5 + 3.0
puts result_addition # => 8.0
# Subtraction
result_subtraction = 7 - 2
puts result_subtraction # => 5
# Multiplication
result_multiplication = 4 * 6.0
puts result_multiplication # => 24.0
# Division
result_division = 15 / 3
puts result_division # => 5
# Modulus (remainder after division)
result_modulus = 17 % 5
puts result_modulus # => 2
# Exponentiation
result_exponentiation = 2**3
puts result_exponentiation # => 8
Comparison Operators
Comparison operators in Ruby are used to compare values and return a boolean result.
# Equal to
puts 5 == 5 # true
# Not equal to
puts 5 != 3 # true
# Less than
puts 3 < 5 # true
# Greater than
puts 5 > 3 # true
# Less than or equal to
puts 3 <= 5 # true
# Greater than or equal to
puts 5 >= 3 # true
Assignment Operators
Assignment operators in Ruby are used to assign values to variables.
# Assignment
x = 5
puts x # => 5
# Add and assign
x += 3
puts x # => 8
# Subtract and assign
x -= 2
puts x # => 6
# Multiply and assign
x *= 4
puts x # => 24
# Divide and assign
x /= 3
puts x # => 8
Logical Operators
Logical operators are used to perform logical operations between boolean values.
# Logical AND
result_and = true && false
puts result_and # => false
# Logical OR
result_or = true || false
puts result_or # => true
# Logical NOT
result_not = !true
puts result_not # => false
Bitwise Operators
Bitwise operators in Ruby are used for manipulation of individual bits in integer values.
# Bitwise AND
result_and = 5 & 3
puts result_and # => 1
# Bitwise OR
result_or = 5 | 3
puts result_or # => 7
# Bitwise XOR (exclusive OR)
result_xor = 5 ^ 3
puts result_xor # => 6
# Bitwise NOT (complement)
result_not = ~5
puts result_not # => -6
# Left shift
result_left_shift = 5 << 2
puts result_left_shift # => 20
# Right shift
result_right_shift = 5 >> 1
puts result_right_shift # => 2
Ternary Operator
The ternary operator in Ruby is a concise way to write a simple conditional expression. The basic syntax is just like C's:
condition ? expression_if_true : expression_if_false
Here's an example:
age = 20
result = (age >= 18) ? "Adult" : "Minor"
puts result # => "Adult"
Range Operators
In Ruby, the range operator is used to create ranges of values. There are two types of range operators: inclusive range (..
) and exclusive range (...
).
inclusive_range = 1..5
# Convert the range to an array
puts inclusive_range.to_a.inspect # => [1, 2, 3, 4, 5]
exclusive_range = 1...5
# Convert the range to an array
puts exclusive_range.to_a.inspect # => [1, 2, 3, 4]
Membership Operators
Membership operators are used to check if an element belongs to a collection.
in?
The in?
operator checks if an element is present in a collection:
fruits = ["apple", "banana", "orange"]
puts "banana".in?(fruits) # true
puts "grape".in?(fruits) # false
!in?
The !in?
operator checks if an element is not present in a collection:
fruits = ["apple", "banana", "orange"]
puts "banana".!in?(fruits) # false
puts "grape".!in?(fruits) # true
Defined Operator
The defined?
operator is used to check if a given variable, method, or block is defined. It returns a string describing the type of the entity if it is defined, and nil
otherwise.
x = 58
if defined?(x)
puts "Variable x is defined."
else
puts "Variable x is not defined."
end
Code Challenge
Write a Ruby function that takes an integer as input and returns the count of set bits (
1
s) in its binary representation. Use bitwise operators to implement this function.